Select Only 5 Random Rows in the Last 50 Entries with MySQL

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

404 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 −mysql> insert into DemoTable1853 values(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ... Read More

Why Comparing Types in MySQL Won't Raise an Error

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

150 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 some records in the table using insert command −mysql> insert into DemoTable1852 values('1John', 1); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1852 values('John', 1); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1852 values('1', 1); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

Delete Records Where Timestamp is Older Than 5 Minutes in MySQL

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 affected (0.00 sec) mysql> insert into DemoTable1851 values('2019-12-03 21:45:00'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1851 values('2019-12-03 21:34:00'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1851; This will produce the following output −+---------------------+ | ... Read More

MySQL IF to Display Custom Yes or No Messages

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

688 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

Replace NULL Values with Empty Strings in MySQL Query

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

2K+ 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

Add Time in MySQL Column with DATETIME Type

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

832 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 MySQL Query

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

478 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

Fetch Records Based on Specific Month and Year in MySQL

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

808 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

Select Records 15 Days Before Today in MySQL

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

973 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

Calculate Percentage in MySQL Query and Round Off Result

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 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

Advertisements