Found 4219 Articles for MySQLi

Concatenate multiple rows and columns in a single row with MySQL

AmitDiwan
Updated on 10-Dec-2019 07:02:27

1K+ Views

To concatenate multiple rows and columns in single row, you can use GROUP_CONCAT() along with CONCAT(). Let us first create a table −mysql> create table DemoTable1463    -> (    -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ClientName varchar(20),    -> ClientAge int    -> ); Query OK, 0 rows affected (1.37 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1463(ClientName, ClientAge) values('Adam Smith', 34); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1463(ClientName, ClientAge) values('John Doe', 29); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1463(ClientName, ClientAge) values('David ... Read More

Perform mathematical calculations in a MySQL table with NULL and NON-NULL values

AmitDiwan
Updated on 10-Dec-2019 07:00:25

175 Views

For this, you can use IFNULL() and perform mathematical calculations with NULL and NON-NULL values. Let us first create a table −mysql> create table DemoTable1462    -> (    -> Value1 int,    -> Value2 int    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1462 values(10, 20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1462 values(50, NULL); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1462 values(NULL, 70); Query OK, 1 row affected (0.25 sec)Display all records from the table using select ... Read More

MySQL ORDER BY 'ENUM' type value based on conditions

AmitDiwan
Updated on 10-Dec-2019 06:56:49

403 Views

For this, use ORDER BY CASE statement. Let us first create a table, wherein we have ENUM type column −mysql> create table DemoTable1461    -> (    -> DeckOfCards ENUM('K', 'J', 'A', 'Q')    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1461 values('K'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1461 values('A'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1461 values('J'); Query OK, 1 row affected (0.44 sec) mysql> insert into DemoTable1461 values('Q'); Query OK, 1 row affected (0.13 sec)Display all ... Read More

Fetch the size of a specific column values in MySQL and display the sum

AmitDiwan
Updated on 10-Dec-2019 06:48:53

84 Views

Let us first create a table −mysql> create table DemoTable1612    -> (    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1612 values('David', 'Brown'); Query OK, 1 row affected (0.75 sec) mysql> insert into DemoTable1612 values('John', 'Smith'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1612 values('Bob', 'Taylor'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select * from DemoTable1612;This will produce the following output −+-----------+----------+ | FirstName | ... Read More

Perform case insensitive SELECT using MySQL IN()?

AmitDiwan
Updated on 10-Dec-2019 06:43:45

177 Views

Let us first create a table −mysql> create table DemoTable1460    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.91 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1460 values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1460 values('David'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1460 values('Bob'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable1460 values('Robert'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select * from DemoTable1460;This will produce the following ... Read More

Use UNION ALL to insert records in two tables with a single query in MYSQL

AmitDiwan
Updated on 10-Dec-2019 06:38:17

849 Views

Here is the query to create first table.mysql> create table DemoTable1    -> (    -> StudentName varchar(20),    -> StudentMarks int    -> ); Query OK, 0 rows affected (0.67 sec)To understand the above concept, let us create second table.mysql> create table DemoTable2    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2 values('Chris'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select * from DemoTable2;This will produce the following output −+-------+ ... Read More

Ignore null values in MySQL and display rest of the values

AmitDiwan
Updated on 10-Dec-2019 06:32:49

1K+ Views

Use IS NOT NULL to find the non-null values and display them. Let us first create a table −mysql> create table DemoTable1458    -> (    -> StudentName varchar(20),    -> StudentScore int    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1458 values('Chris Brown', 56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1458 values('David Miller', NULL); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1458 values('John Doe', 78); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1458 values('Adam Smith', NULL); ... Read More

MySQL query to convert timestamp to month?

AmitDiwan
Updated on 10-Dec-2019 06:30:40

637 Views

To convert timestamp to month, use the FROM_UNIXTIME() method as in the below syntax −select month(from_unixtime(yourColumnName)) from yourTableName;Let us first create a table −mysql> create table DemoTable1457    -> (    -> Value bigint    -> ); Query OK, 0 rows affected (0.85 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1457 values(1570207117); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable1457 values(1548947534); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1457 values(1575213134); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select * from ... Read More

Perform MySQL search between two dates

AmitDiwan
Updated on 10-Dec-2019 06:23:04

384 Views

To perform MySQL search between two dates, use BETWEEN keyword. Let us first create a table −mysql> create table DemoTable1456    -> (    -> CustomerName varchar(30),    -> StartOfferDate  date,    -> EndOfferDate date    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1456 values('Chris', '2019-09-01', '2019-09-30'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1456 values('David', '2019-09-01', '2019-10-30'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1456 values('Bob', '2018-10-01', '2018-10-20'); Query OK, 1 row affected (0.15 sec)Display all records from the table ... Read More

Perform MySQL SELECT on fields containing null values?

AmitDiwan
Updated on 10-Dec-2019 06:18:58

61 Views

To check for NULL values in SELECT, use MySQL NULL. Let us first create a table −mysql> create table DemoTable1455    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1455 values('John'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1455 values(NULL); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1455 values(''); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select * from DemoTable1455;This will produce the following output −+------+ ... Read More

Advertisements