Found 4381 Articles for MySQL

How to order by a custom rule like order like 4,2,1,3 in MySQL?

George John
Updated on 30-Jul-2019 22:30:26

271 Views

To order with a custom rule, use the ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable    (    Number int    ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(4); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(2); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(3); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> ... Read More

Combining multiple rows into a comma delimited list in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

4K+ Views

To combine multiple rows into a comma delimited list, use the GROUP_CONCAT() method. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(30),    Marks int    ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Marks) values('John', 67); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(Name, Marks) values('Carol', 69); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Name, Marks) values('Sam', 69); Query OK, 1 row affected (0.12 sec) mysql> insert ... Read More

MySQL ORDER BY with different ordering for some of the values as descending and others ascending?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

172 Views

You can use the field() for this. Let us first create a table −mysql> create table DemoTable    (    Value int    ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(81); Query OK, 1 row affected (0.44 sec) mysql> ... Read More

How to check whether now() falls between two specific dates in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

493 Views

Here, now() represents the current date. To check whether it falls between two specific dates, you need to use the BETWEEN. Let us first create a table −mysql> create table DemoTable    (    FirstDate datetime,    SecondDate datetime    ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-04-01', '2019-05-02'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('2019-05-28', '2019-06-04'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('2016-01-31', '2019-03-01'); Query OK, 1 row affected (0.15 sec)Display all records from the table ... Read More

Get MAX() on column in two MySQL tables?

George John
Updated on 30-Jul-2019 22:30:26

528 Views

Use GREATEST() to find the maximum. Let us first create a table −mysql> create table DemoTable1    (    Number int    ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(80); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1 values(229); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(575); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+--------+ | Number | +--------+ | 80 | | 229 ... Read More

Sorted difference between two columns in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

517 Views

Use ORDER BY clause for this. Let us first create a table −mysql> create table DemoTable    (    Value1 int,    Value2 int    ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 40); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(50, 5); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(51, 56); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(52, 78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(90, 7); Query ... Read More

MySQL select to count values equal to 0 and greater than 0 from a column?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

For this, use the CASE statement. Let us first create a table −mysql> create table DemoTable    (    Number int    ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(50); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.09 sec) mysql> insert ... Read More

Can we update a row with the highest ID in a single MySQL query?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

270 Views

Yes, we can do that. Let us first create a table −mysql> create table DemoTable    (    ID int,    GameScore int    ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(15, 848747); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(13, 909049); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(34, 98474646); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(31, 948474); Query OK, 1 row affected (0.27 sec)Display all records from the table using select statement ... Read More

Resolve Syntax error near “ORDER BY order DESC” in MySQL?

George John
Updated on 30-Jul-2019 22:30:26

3K+ Views

The word order is a reserved order in MySQL and you have used it in the query. To get rid of the syntax error, you need to use backticks(` `) around the order.The correct syntax is as follows −select *from yourTableName ORDER BY `order` DESC;Let us first create a table −mysql> create table DemoTable    (    `order` int    ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(67); Query OK, 1 row affected (0.13 sec) ... Read More

MySQL query to alphabetize records and count the duplicates?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

154 Views

For this, use both GROUP BY and ORDER BY clause. Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentGrade char(1)    ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentGrade) values('A'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentGrade) values('F'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentGrade) values('C'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable(StudentGrade) values('A'); Query OK, 1 row affected (0.23 sec) mysql> insert ... Read More

Advertisements