Found 4219 Articles for MySQLi

Display distinct column name in MySQL

AmitDiwan
Updated on 02-Jan-2020 05:29:29

145 Views

Let us create a table −mysql> create table DemoTable1996 (    ShippingDate datetime,    CustomerName varchar(20) ); Query OK, 0 rows affected (0.84 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1996 values('2019-12-21 10:45:00', 'Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1996 values('2019-12-21 12:10:00', 'David'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1996 values('2019-12-20 12:10:00', 'Bob'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select * from DemoTable1996;This will produce the following output −+---------------------+--------------+ | ShippingDate        | CustomerName ... Read More

Add 11 days to current date in MySQL

AmitDiwan
Updated on 02-Jan-2020 05:27:36

333 Views

Let us first create a table −mysql> create table DemoTable1994 (    ArrivalDate date ); Query OK, 0 rows affected (5.33 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1994 values('2019-12-18'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable1994 values('2019-12-19'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable1994 values('2019-12-20'); Query OK, 1 row affected (0.49 sec) mysql> insert into DemoTable1994 values('2019-12-25'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable1994 values('2018-12-20'); Query OK, 1 row affected (1.42 sec)Display all records from the table using select statement −mysql> select ... Read More

Set custom messages for enum values in MySQL

AmitDiwan
Updated on 02-Jan-2020 05:25:01

118 Views

Use the if else to set custom messages for enum. Let us first create a table −mysql> create table DemoTable1992 (    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(20),    isActive ENUM('Y', 'N') ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1992(ClientName, isActive) values('Chris', 'N'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1992(ClientName, isActive) values('Bob', 'N'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1992(ClientName, isActive) values('David', 'Y'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1992(ClientName, isActive) values('Carol', ... Read More

Select from table where value does not exist with MySQL?

AmitDiwan
Updated on 02-Jan-2020 05:22:00

644 Views

For this, you can use NOT IN() −mysql> create table DemoTable1991 (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20) ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1991(StudentName) values('Chris'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1991(StudentName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1991(StudentName) values('David'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1991(StudentName) values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1991(StudentName) values('Mike'); Query OK, 1 row affected (0.11 sec)Display all ... Read More

MySQL - Select dates that are one week ahead from today?

AmitDiwan
Updated on 02-Jan-2020 05:19:12

241 Views

To get dates that are one week ahead from today, use DATEDIFF. Let us first get the current date −mysql> select curdate(); +------------+ | curdate()  | +------------+ | 2019-12-20 | +------------+ 1 row in set (0.00 sec)We will first create a table −mysql> create table DemoTable1990    (    ShippingDate date    ); Query OK, 0 rows affected (0.99 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1990 values('2019-12-13'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1990 values('2019-12-21'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1990 values('2019-12-20'); Query OK, 1 ... Read More

Convert VARCHAR data to MySQL date format?

AmitDiwan
Updated on 02-Jan-2020 05:17:18

557 Views

To convert VARCHAR data to date format, you can use STR_TO_DATE() −mysql> create table DemoTable1989    (    DueDate varchar(20)    ); Query OK, 0 rows affected (0.91 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1989 values('31/01/2015'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable1989 values('01/12/2018'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1989 values('25/10/2019'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select * from DemoTable1989;This will produce the following output −+------------+ | DueDate    | +------------+ | 31/01/2015 | ... Read More

Set an alternative of WHERE clause for each SELECT field in MySQL

AmitDiwan
Updated on 02-Jan-2020 05:15:50

395 Views

You can use CASE statement −mysql> create table DemoTable1988    (    Value1 int,    Value2 int,    Price int    ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1988 values(10, 7, 500); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1988 values(7, 9, 400); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1988 values(8, 7, 200); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1988 values(7, 4, 300); Query OK, 1 row affected (0.16 sec)Display all records from the table using select ... Read More

Convert DATE timestamp to return the month number

AmitDiwan
Updated on 02-Jan-2020 05:13:03

150 Views

To return only the month number, you can use DATE_FORMAT() -mysql> create table DemoTable1999    (    ArrivalDate timestamp    ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1999 values('2019-01-01 12:34:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1999 values('2019-12-31 10:04:00'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1999 values('2018-10-11 04:04:30'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select * from DemoTable1999;This will produce the following output −+---------------------+ | ArrivalDate         ... Read More

Set multiple values for custom columns in MySQL?

AmitDiwan
Updated on 31-Dec-2019 08:14:56

398 Views

For this, you can use UNION ALL. Let us first create a table −mysql> create table DemoTable1987    (    UserValue int    ); Query OK, 0 rows affected (2.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1987 values(4); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1987 values(5); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1987 values(6); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1987 values(7); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select * from DemoTable1987;This ... Read More

Only display specified values inside the IN clause with MySQL?

AmitDiwan
Updated on 31-Dec-2019 08:13:56

69 Views

For this, you can use IN() along with ORDER BY clause. Let us first create a table −mysql> create table DemoTable1986    (    Number int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1986 values(50); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1986 values(60); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1986 values(100); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1986 values(200); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1986 values(350); Query OK, 1 row ... Read More

Advertisements