AmitDiwan has Published 10744 Articles

Order MySQL query by multiple ids?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:31:48

315 Views

For this, use ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable(    ClientId varchar(40),    ClientName varchar(40) ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('987_John', 'John'); Query OK, 1 row affected (0.33 ... Read More

Display Timestamp before the current date in MySQL

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:29:31

148 Views

Let us first create a table −mysql> create table DemoTable(    ArrivalDate timestamp ); Query OK, 0 rows affected (1.96 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-09-14 17:25:00'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('2019-09-13 17:25:00'); Query OK, ... Read More

MySQL query to concatenate all the values in each row based on the common matching ID

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:25:36

158 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int,    StudentFirstName varchar(100),    StudentLastName varchar(100) ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1000, 'Adam', 'Smith'); Query OK, 1 row affected (0.17 sec) ... Read More

MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:22:26

504 Views

For this, use SUM() along with GROUP BY. Let us first create a table −mysql> create table DemoTable (    CustomerName varchar(100),    Product_1_Price int,    Product_2_Price int ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 67, ... Read More

Display records by first fixing the first two values in a column and then using DISTINCT to display other values in MySQL

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:19:12

86 Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100) ); Query OK, 0 rows affected (0.96 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, ... Read More

How can I count unique records from a column in MySQL database?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:17:32

209 Views

For this, use aggregate function count(*) to count to GROUP BY to group. Let us first create a table −mysql> create table DemoTable (    UserName varchar(100),    UserPostMessage text ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable ... Read More

MySQL query to display records ordered by DESC while skipping some?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:14:25

98 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.94 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 'Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable ... Read More

Find the next lowest number higher than a certain number in MySQL?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:11:28

274 Views

For this, use aggregate function MIN() along with WHERE clause. Let us first create a table −mysql> create table DemoTable (    Number int ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(11); Query OK, 1 row affected ... Read More

MySQL query to change a string by displaying only the part of string after underscore?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:04:32

136 Views

Let us first create a table −mysql> create table DemoTable (    UserName varchar(100) ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Smith_John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Smith_Adam'); Query OK, 1 ... Read More

MySQL query to find expiry date (record) from the next 2 days?

AmitDiwan

AmitDiwan

Updated on 27-Sep-2019 07:01:58

2K+ Views

For this, you can use BETWEEN keyword. Let us first create a table −mysql> create table DemoTable (    ExpiryDate date ); Query OK, 0 rows affected (0.55 sec)Note − Let’s say the current date is 2019-08-18.Insert some records in the table using insert command −mysql> insert into DemoTable values('2018-01-21'); ... Read More

Advertisements