Found 4219 Articles for MySQLi

Which MySQL Datatype should be used for storing BloodType?

AmitDiwan
Updated on 26-Dec-2019 06:35:52

812 Views

To store BloodType, use varchar(3) or ENUM. Let us first create a table −mysql> create table DemoTable1855      (      BloodType varchar(3)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1855 values('A+'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1855 values('A-'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1855 values('B+'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1855 values('B-'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1855 values('AB+'); Query OK, 1 row affected ... Read More

Distinct number of specific items in list with MySQL

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

145 Views

To find distinct number of specific items, use COUNT() along with GROUP BY clause. Let us first create a table −mysql> create table DemoTable1854      (      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1854 values('John-Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1854 values('Chris-Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1854 values('Adam-Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1854 values('John-Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

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

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

296 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 does comparing types in MySQL won’t raise an error?

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

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

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

991 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

Advertisements