MySQL order from a different starting value?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
   (
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(30)
   );
Query OK, 0 rows affected (0.74 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable(ClientName) values('John');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(ClientName) values('Chris');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(ClientName) values('Robert');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable(ClientName) values('David');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(ClientName) values('Bob');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(ClientName) values('Carol');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(ClientName) values('Sam');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable(ClientName) values('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 −

+----------+------------+
| ClientId | ClientName |
+----------+------------+
| 1        | John       |
| 2        | Chris      |
| 3        | Robert     |
| 4        | David      |
| 5        | Bob        |
| 6        | Carol      |
| 7        | Sam        |
| 8        | Mike       |
+----------+------------+
8 rows in set (0.00 sec)

Following is the query of MySQL Order from a starting value −

mysql> select *from DemoTable order by ClientId < 5,ClientId;

This will produce the following output −

+----------+------------+
| ClientId | ClientName |
+----------+------------+
| 5        | Bob        |
| 6        | Carol      |
| 7        | Sam        |
| 8        | Mike       |
| 1        | John       |
| 2        | Chris      |
| 3        | Robert     |
| 4        | David      |
+----------+------------+
8 rows in set (0.00 sec)
raja
Published on 03-May-2019 10:53:31
Advertisements