AmitDiwan has Published 10744 Articles

Implement Custom Sort Order in MySQL

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:36:38

2K+ Views

To implement custom sort order in MySQL, you need to use ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable    -> (    -> Designation varchar(100)    -> ); Query OK, 0 rows affected (1.65 sec)Insert some records in the table using insert command −mysql> ... Read More

Subtracting a day in MySQL

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:35:07

316 Views

To subtract a day in MySQL, use the DATE_SUB() method. Let us first create a table −mysql> create table DemoTable    -> (    -> AdmissionDate timestamp    -> ); Query OK, 0 rows affected (1.05 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-01'); ... Read More

Can we select field name in MySQL that contains an asterisk?

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:33:51

344 Views

No, we cannot. To still work it out, use backticks around the field name. Let us first create a table with column name with asterisk, `Name*` −mysql> create table DemoTable    -> (    -> `Name*` varchar(20)    -> ); Query OK, 0 rows affected (2.03 sec)Insert some records in ... Read More

MySQL query to search a string ‘demo’ if someone mistakenly types ‘deom’?

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:32:18

133 Views

For this, you can use SOUND along with the LIKE operator. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.95 sec)Insert some records in the table using insert command −mysql> insert into DemoTable ... Read More

How to replace 'Empty set' in a MySQL query?

AmitDiwan

AmitDiwan

Updated on 13-Dec-2019 05:31:06

724 Views

To replace a record that doesn’t exist, use the COALESCE in MySQL. The COALESCE would help in substituting the NULL values. Let us first create a table −mysql> create table DemoTable    -> (    -> Code varchar(20)    -> ); Query OK, 0 rows affected (1.64 sec)Insert some records ... Read More

Order by a single field and display rest of the records in the same order with MySQL

AmitDiwan

AmitDiwan

Updated on 12-Dec-2019 07:44:20

191 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(201, 'Chris Brown'); Query OK, 1 row ... Read More

Find “greatest” between two columns and display with some records already null in MySql

AmitDiwan

AmitDiwan

Updated on 12-Dec-2019 07:41:07

140 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Value1 int,    -> Value2 int    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(78, 89); Query OK, 1 row affected ... Read More

MySQL case statement inside a select statement?

AmitDiwan

AmitDiwan

Updated on 12-Dec-2019 07:38:42

473 Views

For this, you can use CASE WHEN statement. Let us first create a table −mysql> create table DemoTable    -> (    -> FirstName varchar(20),    -> Score int -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> ... Read More

Order records and delete n rows in MySQL

AmitDiwan

AmitDiwan

Updated on 12-Dec-2019 07:35:13

169 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('Chris'); Query ... Read More

Find the difference between two datetime values with MySQL?

AmitDiwan

AmitDiwan

Updated on 12-Dec-2019 07:29:24

417 Views

To find the difference between two datetime values, you can use TIMESTAMPDIFF(). Let us first create a table −mysql> create table DemoTable    -> (    -> DueDatetime1 datetime,    -> DueDatetime2 datetime    -> ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert ... Read More

Advertisements