AmitDiwan has Published 10744 Articles

Select only 5 random rows in the last 50 entries With MySQL?

AmitDiwan

AmitDiwan

Updated on 26-Dec-2019 06:31:46

394 Views

For this, use ORDER BY RAND() with subquery. Let us first create a table −mysql> create table DemoTable1853      (      UserId int NOT NULL AUTO_INCREMENT,      PRIMARY KEY(UserId)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command ... Read More

Why does comparing types in MySQL won’t raise an error?

AmitDiwan

AmitDiwan

Updated on 26-Dec-2019 06:30:23

145 Views

If you try to compare string to int, MySQL won’t raise an error because it converts string to int. Let us first create a table −mysql> create table DemoTable1852      (      Value1 varchar(20),      Value2 int      ); Query OK, 0 rows affected (0.00 sec)Insert ... Read More

Delete records where timestamp older than 5 minutes in MySQL?

AmitDiwan

AmitDiwan

Updated on 26-Dec-2019 06:28:28

2K+ Views

For this, use DELETE command. Let us first create a table −mysql> create table DemoTable1851      (      DueDate datetime      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1851 values('2019-12-03 21:30:35'); Query OK, 1 row ... Read More

MySQL IF() to display custom YES or NO messages

AmitDiwan

AmitDiwan

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

675 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 ... Read More

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

AmitDiwan

AmitDiwan

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

1K+ 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 ... Read More

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

AmitDiwan

AmitDiwan

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

826 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 ... Read More

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

AmitDiwan

AmitDiwan

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

471 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> ... Read More

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

AmitDiwan

AmitDiwan

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

801 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 ... Read More

Selecting records 15 days before today in MySQL?

AmitDiwan

AmitDiwan

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

957 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 ... Read More

Calculating percentage in a MySQL query and round off the result

AmitDiwan

AmitDiwan

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

4K+ 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 ... Read More

Advertisements