To set specific record ordering, use ORDER BY LIKE. Let us first create a table−
mysql> create table DemoTable808(Value varchar(100)); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable808 values('smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable808 values('Adamsmith'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable808 values('Carolsmith'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable808 values('smithJohn'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable808;
This will produce the following output −
+------------+ | Value | +------------+ | smith | | Adamsmith | | Carolsmith | | smithJohn | +------------+ 4 rows in set (0.00 sec)
Following is the query to set specific ordering −
mysql> select *from DemoTable808 order by case when Value like 'smith%' then 0 else 1 end asc,Value asc;
This will produce the following output −
+------------+ | Value | +------------+ | smith | | smithJohn | | Adamsmith | | Carolsmith | +------------+ 4 rows in set (0.00 sec)