要按第一个字母排序,请使用ORDER BY CASE语句。让我们首先创建一个表-
mysql> create table DemoTable1535 -> ( -> Value varchar(100) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1535 values('MySQL is good relational database.'); mysql> insert into DemoTable1535 values('is MySQL easy to lean'); mysql> insert into DemoTable1535 values('You need to start basic SQL');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1535;
这将产生以下输出-
+------------------------------------+ | Value | +------------------------------------+ | MySQL is good relational database. | | is MySQL easy to lean | | You need to start basic SQL | +------------------------------------+ 3 rows in set (0.00 sec)
以下是按首字母排序的查询-
mysql> select * from DemoTable1535 -> order by case when left(Value, 1) = 'i' then 1 else 2 end,Value;
这将产生以下输出-
+------------------------------------+ | Value | +------------------------------------+ | is MySQL easy to lean | | MySQL is good relational database. | | You need to start basic SQL | +------------------------------------+ 3 rows in set (0.00 sec)