Implement MySQL Void Query That Does Nothing

AmitDiwan
Updated on 03-Sep-2019 12:12:28

310 Views

Let us first create a table −mysql> create table DemoTable(Name varchar(100)); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+ | Name   | +--------+ | Chris  | ... Read More

Update Last Entry of a Particular ID in MySQL

AmitDiwan
Updated on 03-Sep-2019 12:11:13

226 Views

To update the last entry of a particular ID in MySQL, use ORDER BY DESC LIMIT 1. This would allow you to update the last entry i.e. row records.Let us first create a table −mysql> create table DemoTable824(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(100),    ClientAge int ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable824(ClientName, ClientAge) values('Chris', 23); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable824(ClientName, ClientAge) values('Robert', 26); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable824(ClientName, ... Read More

Retrieve Min and Max Date in a Single MySQL Query

AmitDiwan
Updated on 03-Sep-2019 12:09:18

4K+ Views

For this, you can use aggregate function MIN() and MAX(). Let us first create a table −mysql> create table DemoTable(AdmissionDate date); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2016-11-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-04-11'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2016-02-01'); Query OK, 1 row affected (0.28 sec)Display all records from the ... Read More

Order By Specific Field Value First in MySQL

AmitDiwan
Updated on 03-Sep-2019 11:58:33

412 Views

To order by specific field value first in MySQL, use ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable849(Color varchar(100)); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable849 values('RED'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable849 values('ORANGE'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable849 values('BLUE'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable849 values('GREEN'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable849;This will ... Read More

Find Records with a Specific Last Digit in MySQL

AmitDiwan
Updated on 03-Sep-2019 11:56:39

1K+ Views

For this, use the RIGHT() method to fetch records with a specific last digit. Let us first create a table −mysql> create table DemoTable823(Value varchar(100)); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable823 values('9847826'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable823 values('84747464'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable823 values('9899889883'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable823 values('123456'); Query OK, 1 row affected (0.10 sec)Display all records from the table using select statement −mysql> select *from DemoTable823;This ... Read More

MySQL Select Products Where Average Price Per Product Value

AmitDiwan
Updated on 03-Sep-2019 11:55:34

478 Views

Let us first create a table −mysql> create table DemoTable848(    ProductId int,    ProductPrice int ); Query OK, 0 rows affected (1.20 sec)Insert some records in the table using insert command −mysql> insert into DemoTable848 values(100, 30); Query OK, 1 row affected (0.57 sec) mysql> insert into DemoTable848 values(101, 50); Query OK, 1 row affected (1.06 sec) mysql> insert into DemoTable848 values(100, 40); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable848 values(101, 25); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable848 values(100, 20); Query OK, 1 row affected (0.31 sec)Display all records from ... Read More

Order By a Specific Word in MySQL

AmitDiwan
Updated on 03-Sep-2019 11:53:51

466 Views

For this, use ORDER BY INSTR(). Let us first create a table −mysql> create table DemoTable822(Word text); Query OK, 0 rows affected (1.11 sec)Insert some records in the table using insert command −mysql> insert into DemoTable822 values('Forever'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable822 values('ever'); Query OK, 1 row affected (1.31 sec) mysql> insert into DemoTable822 values('every'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable822 values('everyday'); Query OK, 1 row affected (0.58 sec)Display all records from the table using select statement −mysql> select *from DemoTable822;This will produce the following output −+----------+ | Word ... Read More

MySQL Query to Exclude Values with Specific Last 3 Digits

AmitDiwan
Updated on 03-Sep-2019 11:52:11

281 Views

For this, use NOT IN. Let us first create a table −mysql> create table DemoTable(Value int); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1234); Query OK, 1 row affected (0.54 sec) mysql> insert into DemoTable values(2345); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(7896); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(4321); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+ | Value | ... Read More

Set Current Date in Datetime Field for All Column Values in MySQL

AmitDiwan
Updated on 03-Sep-2019 11:50:26

187 Views

Let us first create a table −mysql> create table DemoTable821(AdmissionDate datetime); Query OK, 0 rows affected (1.24 sec)Insert some records in the table using insert command −mysql> insert into DemoTable821 values('2019-01-21'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable821 values('2018-11-02'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable821 values('2016-12-31'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable821 values('2015-03-19'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable821;This will produce the following output −+---------------------+ | AdmissionDate ... Read More

Limit Records to Last Five Results in MySQL

AmitDiwan
Updated on 03-Sep-2019 11:48:13

323 Views

To fetch only the last five records below is the syntax −select *from yourTableName order by yourColumnName DESC LIMIT 5;Let us first create a table −mysql> create table DemoTable820(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    CustomerName varchar(100) ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable820(CustomerName) values('Chris'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable820(CustomerName) values('Robert'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable820(CustomerName) values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable820(CustomerName) values('Bob'); Query OK, 1 ... Read More

Advertisements