Found 4381 Articles for MySQL

Insert multiple rows in a single MySQL query

AmitDiwan
Updated on 11-Nov-2019 10:10:16

486 Views

Let us first create a table −mysql> create table DemoTable1384    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentAge int    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command. Here, we are inserting multiple rows in a single query −mysql> insert into DemoTable1384(StudentName, StudentAge) values('Chris Brown', 21), ('David Miller', 22), -> ('Carol Taylor', 19), ('Adam Smith', 23); Query OK, 4 rows affected (0.11 sec) Records: 4  Duplicates: 0  Warnings: 0Display all records from the table using select statement −mysql> select * ... Read More

MySQL query to find the top two highest scores

AmitDiwan
Updated on 11-Nov-2019 10:08:17

556 Views

For this, use aggregate function MAX(). Let us first create a table −mysql> create table DemoTable1383    -> (    -> Id int,    -> PlayerScore int    -> ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1383 values(200, 78); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1383 values(200, 89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1383 values(200, 89); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1383 values(200, 87); Query OK, 1 row affected (0.29 sec) mysql> insert into ... Read More

How to update a MySQL table by swapping two column values?

AmitDiwan
Updated on 11-Nov-2019 10:06:56

463 Views

To swap two values in a column, use CASE WHEN statement. Let us first create a table −mysql> create table DemoTable1382    -> (    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1382 values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1382 values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1382 values('Adam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1382 values('Bob'); Query OK, 1 row affected (0.17 sec)Display all records from the table using ... Read More

How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?

AmitDiwan
Updated on 11-Nov-2019 10:04:18

717 Views

For maximum value, use MAX() along with CAST() for conversion. Since we want maximum value from string-numbers beginning with specific characters, use RLIKE. Let us first create a table −mysql> create table DemoTable1381    -> (    -> DepartmentId varchar(40)    -> ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1381 values('IT794'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1381 values('AT1034'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable1381 values('IT967'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1381 values('IT874'); Query ... Read More

MySQL UNION SELECT and IN clause in a single query

AmitDiwan
Updated on 11-Nov-2019 09:37:50

240 Views

Let us first create a table −mysql> create table DemoTable1    -> (    -> StudentId int,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (1.24 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(210, 'Adam'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select * from DemoTable1;This will produce the following output −+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ |       210 | Adam        | +-----------+-------------+ 1 row in set (0.00 sec)Here is the query to create ... Read More

How can I view cascades in MySQL?

AmitDiwan
Updated on 11-Nov-2019 09:35:27

211 Views

To view cascades, use SHOW CREATE TABLE in MySQL. Let us first create a table −mysql> create table DemoTable1378    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeId varchar(20),    -> EmployeeFirstName varchar(20),    -> EmployeeLastName varchar(20),    -> EmployeeCountryName varchar(40),    -> EmployeeAge int,    -> EmployeeSalary int,    -> UNIQUE(EmployeeFirstName, EmployeeSalary),    -> INDEX First_Last_NameIndex(EmployeeFirstName, EmployeeLastName)    -> ); Query OK, 0 rows affected (0.93 sec)Let us now view cascades in MySQL −mysql> show create table DemoTable1378;This will produce the following output −+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table         | Create Table ... Read More

How to add a year and two days to a date with a single MySQL query?

AmitDiwan
Updated on 11-Nov-2019 09:33:11

165 Views

For this, use INTERVAL in MySQL. Let us first create a table −mysql> create table DemoTable1376    -> (    -> AdmissionDate date    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1376 values('2018-01-21'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1376 values('2017-12-01'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1376 values('2018-11-02'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1376 values('2019-03-14'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement−mysql> select * from ... Read More

The MySQL EXPLAIN keyword executes the query or just explains the query?

AmitDiwan
Updated on 11-Nov-2019 09:31:40

131 Views

The EXPLAIN keyword tells how MySQL executes the query. Let us first create a table −mysql> create table DemoTable1375    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(20),    -> INDEX FIRST_INDEX(FirstName)    -> ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1375(FirstName) values('Chris'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1375(FirstName) values('Bob'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1375(FirstName) values('Sam'); Query OK, 1 row affected (1.06 sec) mysql> insert into DemoTable1375(FirstName) values('David'); Query OK, ... Read More

How to use count with CASE condition in a MySQL query?

AmitDiwan
Updated on 11-Nov-2019 09:27:50

3K+ Views

Use CASE WHEN for this in MySQL and set CASE condition inside the COUNT() method to count. Let us first create a table −mysql> create table DemoTable1374    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20),    -> Score int    -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1374(Name, Score) values('Chris', 45); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1374(Name, Score) values('David', 78); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1374(Name, Score) values('Bob', 45); ... Read More

Update a MySQL column with JSON format?

AmitDiwan
Updated on 11-Nov-2019 09:25:42

984 Views

To display records like JSON format, use MySQL concat(). Let us first create a table −mysql> create table DemoTable1373    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentDetails text    -> ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command. Here, we haven’t inserted anything −mysql> insert into DemoTable1373 values(); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1373 values(); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1373 values(); Query OK, 1 row affected (0.18 sec)Display all records from the table using ... Read More

Advertisements