AmitDiwan has Published 10744 Articles

How to select next row pagination in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:52:43

381 Views

For this, use the LIMIT concept. Let us create a table −mysql> create table demo40 −> ( −> id int not null auto_increment primary key, −> name varchar(40) −> ); Query OK, 0 rows affected (1.73 sec)Insert some records into the table with the help of insert command −mysql> insert ... Read More

How to select rows with condition via concatenate in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:47:01

255 Views

For this, you can use CONCAT_WS(). Let us create a table −mysql> create table demo38 −> ( −> user_id int, −> user_first_name varchar(20), −> user_last_name varchar(20), −> user_date_of_birth date −> ); Query OK, 0 rows affected (1.70 sec)Insert some records into the table with the help of insert command −mysql> ... Read More

Using Prepared statement correctly with WHERE condition in case of any value in MySQL Java

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:44:45

1K+ Views

For this, you can use PrepareStatement in Java. Following is the syntax −String anyVariableName="select yourColumnName from yourTableName where name = ?"; PreparedStatement ps = (PreparedStatement) con.prepareStatement(yourVariableName); ps.setString(yourColumnIndex, yourValue);Let us create a table −mysql> create table demo37 −> ( −> id int not null auto_increment primary key, −> name varchar(200) −> ... Read More

How to count the number of columns having specific value in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:41:51

328 Views

Following is the syntax −select sum(yourColumnName1+yourColumnName2+yourColumnName3...N) as `anyAliasName1`, sum(yourColumnName1 and yourColumnName2 and yourColumnName3….N) as anyAliasName from yourTableName;Let us create a table −mysql> create table demo36 −> ( −> id int not null auto_increment primary key, −> value1 int, −> value2 int, −> value3 int −> ); Query OK, 0 rows ... Read More

How to select between/before/after dates in MySQL conditionally?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:39:16

2K+ Views

Following is the syntax −select *from yourTableName where yourColumnName1 < yourValue1 AND (yourColumnName2 > yourValue2 OR yourColumnName2 is null);Let us create a table −mysql> create table demo35 −> ( −> id int NOT NULL AUTO_INCREMENT PRIMARY KEY, −> joining_date date, −> relieving_date date −> ); Query OK, 0 rows affected ... Read More

Extract Numeric Date Value from Date Format in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:37:16

306 Views

For this, use UNIX_TIMESTAMP(). Following is the syntax −select UNIX_TIMESTAMP(STR_TO_DATE(yourColumnName, "%d-%b-%y")) as anyAliasName from yourTableName;Let us create a table −mysql> create table demo34 −> ( −> datevalue varchar(40) −> ); Query OK, 0 rows affected (1.51 sec)Insert some records into the table with the help of insert command −mysql> insert ... Read More

Create a new table in MySQL with specific options with DEFAULT?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:36:03

150 Views

For this, use DEFAULT keyword after the column data type.Let us create a table −mysql> create table demo33 −> ( −> id int not null auto_increment primary key, −> name varchar(20) not null, −> start_date date default(current_date), −> end_date date default NULL, −> category enum('Good', 'Medium', 'Low') default 'Low' −> ... Read More

How to change a table (create/alter) so that the calculated “Average score” field is shown when querying the entire table without using MySQL INSERT, UPDATE?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:34:11

109 Views

Following is the syntax −alter table yourTableName add column yourColumnName yourDataType generated always as ((yourColumName1+yourColumName2+....N) / N) virtual;Let us create a table −mysql> create table demo32 −> ( −> value1 int, −> value2 int −> ); Query OK, 0 rows affected (1.42 sec)Insert some records into the table with the ... Read More

How to calculate an average value across database rows in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:31:38

672 Views

For this, you can use AVG(). Following is the syntax −select avg(yourColumnName1) as anyAliasName1, avg(yourColumnName2) as anyAliasName2, avg(yourColumnName3) as anyAliasName3, . . N from yourTableName;Let us create a table −mysql> create table demo31 −> ( −> value1 int, −> value2 int, −> value3 int −> ); Query OK, 0 rows ... Read More

How to use the name of the current database to delete it in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:28:58

98 Views

To get the current database, you can use the SELECT DATABASE() −select database();Following is the syntax −set @anyVariableName = database(); select @anyVariableName; set @anyVariableName2 = concat('drop database ', @yourVariableName); prepare anyVariableName3 from @yourVariableName2; execute yourVariableName3;Let us execute the above query in order to get current database and delete it −mysql> ... Read More

Advertisements