Found 4378 Articles for MySQL

MySQL IF() to display custom YES or NO messages

AmitDiwan
Updated on 26-Dec-2019 06:27:07

514 Views

Let us first create a table −mysql> create table DemoTable1850      (      OrderStatus varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1850 values('Yes'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1850 values('No'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1850 values('Yes'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1850 values('Yes'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1850; This will produce the ... Read More

MySQL query to replace null value with empty string in several columns while fetching data

AmitDiwan
Updated on 26-Dec-2019 06:26:03

989 Views

For this, you can use IFNULL() or COALESCE(). Let us first create a table −mysql> create table DemoTable1849      (      ClientFirstName varchar(20),      ClientLastName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1849 values('John', NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1849 values(NULL, 'Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1849 values(NULL, NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1849 values('Chris', 'Brown'); Query OK, 1 row affected (0.00 sec)Display all ... Read More

How to add time in a MySQL column set with type DATETIME?

AmitDiwan
Updated on 26-Dec-2019 06:23:48

748 Views

To add time to datetime, use ADDTIME() function in MySQL. Let us first create a table −mysql> create table DemoTable1848      (      ShippingDate datetime      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1848 values('2019-10-11 12:30:45'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1848 values('2019-01-12 10:00:00'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1848 values('2019-12-03 17:30:00'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1848; This will produce ... Read More

Add user defined value to a column in a MySQL query?

AmitDiwan
Updated on 26-Dec-2019 06:22:14

368 Views

Let us first create a table −mysql> create table DemoTable1847      (      GameStatus ENUM('PENDING', 'COMPLETED', 'CANCELLED')      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1847 values('PENDING'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1847 values('COMPLETED'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1847 values('CANCELLED'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1847; This will produce the following output −+------------+ | GameStatus | +------------+ | PENDING   ... Read More

MySQL to fetch records based on a specific month and year?

AmitDiwan
Updated on 26-Dec-2019 06:20:06

663 Views

For this, use MONTH() and YEAR(). Let us first create a table −mysql> create table DemoTable1846      (      PurchaseDate date      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1846 values('2019-01-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2019-12-24'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2018-09-21'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2017-10-26'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * ... Read More

Selecting records 15 days before today in MySQL?

AmitDiwan
Updated on 26-Dec-2019 06:18:48

785 Views

For this, you can use the concept of INTERVAL and DATE_SUB(). Let us first create a table −mysql> create table DemoTable1845      (      ArrivalDate date      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1845 values('2019-12-02'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1845 values('2019-11-18'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1845 values('2019-12-18'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1845 values('2019-12-25'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1845 values('2019-11-15'); Query ... Read More

Calculating percentage in a MySQL query and round off the result

AmitDiwan
Updated on 26-Dec-2019 06:15:48

2K+ Views

For this, you can use CONCAT() and round(). Let us first create a table −mysql> create table DemoTable1844      (      Number int,      TotalNumber int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1844 values(50, 500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(80, 500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(98, 500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(45, 500); Query OK, 1 row affected (0.00 sec)Display all ... Read More

Get beginning and end date from a specific year in MySQL

AmitDiwan
Updated on 26-Dec-2019 06:11:02

393 Views

For this, use MySQL YEAR() function. Let us first create a table −mysql> create table DemoTable1843      (      StartDate date,      EndDate date      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1843 values('2019-01-21', '2019-10-12'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1843 values('2018-10-12', '2018-12-31'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1843 values('2016-04-01', '2017-05-02'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1843;This will produce the ... Read More

Multiplying column with NULL row in MySQL?

AmitDiwan
Updated on 26-Dec-2019 06:04:14

376 Views

To multiply with NULL row, you can use COALESCE(). Let us first create a table −mysql> create table DemoTable1842      (      NumberOfItems int,      Amount int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1842 values(10, 40); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1842 values(20, 5); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1842 values(NULL, 10); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1842;This ... Read More

Return records that do not have a value in a certain field with two SELECT statement in a single MySQL query

AmitDiwan
Updated on 26-Dec-2019 05:59:15

100 Views

For this, you can use WHERE clause along with subquery. Let us first create a table −mysql> create table DemoTable1840      (      UserName varchar(20),      UserType ENUM('GUEST', 'ADMIN')      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1840 values('Chris', 'Admin'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1840 values('David', 'Guest'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1840 values('Chris', 'Guest'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * ... Read More

Advertisements