AmitDiwan has Published 10744 Articles

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

AmitDiwan

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 ... Read More

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

AmitDiwan

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 ... Read More

MySQL UNION SELECT and IN clause in a single query

AmitDiwan

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 ... Read More

How can I view cascades in MySQL?

AmitDiwan

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 ... Read More

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

AmitDiwan

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 ... Read More

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

AmitDiwan

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 ... Read More

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

AmitDiwan

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    -> ); ... Read More

Update a MySQL column with JSON format?

AmitDiwan

AmitDiwan

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

987 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 ... Read More

Subtracting a number from a single MySQL column value?

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 09:23:28

770 Views

For this, just update the table and subtract. Let us first create a table −mysql> create table DemoTable1372    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1372 values(500); Query OK, ... Read More

Get values from all rows and display it a single row separated by comma with MySQL

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 09:21:18

2K+ Views

For this, use GROUP_CONCAT(). Do not use GROUP BY clause, since GROUP_CONTACT() is a better and quick solution.Let us first create a table −mysql> create table DemoTable1371    -> (    -> Id int,    -> CountryName varchar(40)    -> ); Query OK, 0 rows affected (0.89 sec)Insert some records ... Read More

Advertisements