Found 4378 Articles for MySQL

Order MySQL query by multiple ids?

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

252 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 sec) mysql> insert into DemoTable values('1000_Sam', 'Sam'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('777_Carol', 'Carol'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2000_Bob', 'Bob'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select ... Read More

Display Timestamp before the current date in MySQL

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

112 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, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2016-09-01 17:20:10'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values('2019-09-11 12:00:00'); 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 output −+---------------------+ | ... Read More

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

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

108 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) mysql> insert into DemoTable values(1000, 'John', 'Doe'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(1000, '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 following output −+-----------+------------------+-----------------+ | StudentId | StudentFirstName ... 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
Updated on 27-Sep-2019 07:22:26

375 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, 89); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('David', 769, 890); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David', 987, 1000); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('John', 900, 111); Query OK, 1 row affected (0.12 sec)Display ... 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
Updated on 27-Sep-2019 07:19:12

60 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, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------+ | FirstName | ... Read More

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

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

123 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 values('Chris', 'Hi'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('David', 'Hello'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Chris', 'Awesome'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Chris', 'Amazing'); Query OK, 1 row affected (0.17 sec) mysql> insert ... Read More

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

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

60 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 values(1, 'David'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(4, 'Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(6, 'Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(7, 'Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert ... Read More

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

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

215 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 (0.19 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(12); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This ... Read More

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

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

68 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 row affected (0.10 sec) mysql> insert into DemoTable values('Smith_David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Smith_Mike'); 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 following output −+-------------+ | UserName ... Read More

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

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

1K+ 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'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-08-20'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable values('2018-08-20'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('2019-08-21'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select ... Read More

Advertisements