Perform MySQL Update Based on Date Value in Another Column

AmitDiwan
Updated on 24-Sep-2019 14:00:41

215 Views

Let us first create a table −mysql> create table DemoTable(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100),    AdmissionDate date ); Query OK, 0 rows affected (0.42 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, AdmissionDate) values('Chris', '2019-11-21'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(Name, AdmissionDate) values('Mike', '2019-03-11'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name, AdmissionDate) values('Sam', '2018-04-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name, AdmissionDate) values('Carol', '2019-05-01'); Query OK, 1 row affected (0.40 sec)Display all records from the ... Read More

Select Users with Maximum Age Values Using MySQL Subquery

AmitDiwan
Updated on 24-Sep-2019 13:54:18

367 Views

Let us first create a table −mysql> create table DemoTable (    Name varchar(100),    Age int ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 23); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('David', 55); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Bob', 53); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Mike', 54); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('Sam', 55); Query OK, 1 row affected (0.18 sec)Display all records ... Read More

Counting Total and True Condition Values in a MySQL Table

AmitDiwan
Updated on 24-Sep-2019 13:47:06

124 Views

For this, you can use COUNT(). Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following ... Read More

Remove Duplicate Values from MySQL Table Using LEFT JOIN

AmitDiwan
Updated on 24-Sep-2019 13:43:38

593 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert ... Read More

Concatenate Strings from Different Columns in MySQL

AmitDiwan
Updated on 24-Sep-2019 13:41:30

126 Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 'Brown'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Adam', 'Smith'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Carol', 'Taylor'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('David', 'Miller'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

Update Field with Particular Value if Null in MySQL

AmitDiwan
Updated on 24-Sep-2019 13:10:08

1K+ Views

To update a field if it is null, use IS NULL property along with the UPDATE command. Let us first create a table −mysql> create table DemoTable (    StudentScore int ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(56); ... Read More

Insert Values from First Table to Second Table using Two SELECT Statements in a Single MySQL Query

AmitDiwan
Updated on 24-Sep-2019 13:08:03

304 Views

To insert values from the first table to another table using two SELECT statements, use SUBQUERY. This will allow you to use only a single MySQL query to get the result in the second table. Let us first create a table −mysql> create table DemoTable1 (    Name varchar(100),    Score int ); Query OK, 0 rows affected (1.30 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values('Chris', 45); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values('Bob', 78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values('David', 98); ... Read More

Order By Field with Group By in a Single MySQL Query

AmitDiwan
Updated on 24-Sep-2019 13:05:27

316 Views

For this, let us first create a table −mysql> create table DemoTable (    Message text ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Good'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values('Bye'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Awesome'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Bye'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Good'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('Amazing'); Query OK, 1 ... Read More

Set MySQL Default Value to None

AmitDiwan
Updated on 24-Sep-2019 13:03:19

785 Views

To set the default value in MySQL, you need to use the DEFAULT keyword. Let us first create a table −mysql> create table DemoTable (    ClientCountryName varchar(100) DEFAULT 'NONE' ); Query OK, 0 rows affected (0.65 sec)We have set DEFAULT above for values not entered while insertion. Now, let us insert some records in the table using the insert command. We haven’t inserted values here for some of the rows. The DEFAULT gets set there −mysql> insert into DemoTable values('US'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.09 sec) ... Read More

Fetch 5 Characters from Left of String using MySQL SELECT Statement

AmitDiwan
Updated on 24-Sep-2019 13:01:07

258 Views

To fetch number of characters from the left of the string, use the LEFT method in MySQL. Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (6.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Sam Brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Adam Smith'); Query OK, 1 row affected (7.01 sec) mysql> insert into DemoTable values('Carol Taylor'); Query OK, 1 row affected (0.68 sec)Display all ... Read More

Advertisements