有关大写的 MySQL Order by?
为此,请使用 order by nullif()。我们先创建一个表 −
mysql> create table DemoTable672( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerName varchar(100), CustomerAmount int ); Query OK, 0 rows affected (0.81 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable672(CustomerName,CustomerAmount) values('Chris',560); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable672(CustomerName,CustomerAmount) values('Robert',null); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable672(CustomerName,CustomerAmount) values('',450); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable672(CustomerName,CustomerAmount) values('David',456); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable672(CustomerName,CustomerAmount) values('Carol',786); Query OK, 1 row affected (0.14 sec)
使用 select 语句在表中显示所有记录 −
mysql> select *from DemoTable672;
这会产生以下输出 −
+------------+--------------+----------------+ | CustomerId | CustomerName | CustomerAmount | +------------+--------------+----------------+ | 1 | Chris | 560 | | 2 | Robert | NULL | | 3 | | 450 | | 4 | David | 456 | | 5 | Carol | 786 | +------------+--------------+----------------+ 5 rows in set (0.00 sec)
以下是执行 MySQL order by 的 case 方式。这里,我们还使用了 FIELD() 进行不区分大小写的搜索 −
mysql> select *from DemoTable672 ORDER BY NULLIF(CustomerName,'') IS NULL, FIELD(CustomerAmount,560,786,450,456);
这会产生以下输出 −
+------------+--------------+----------------+ | CustomerId | CustomerName | CustomerAmount | +------------+--------------+----------------+ | 2 | Robert | NULL | | 1 | Chris | 560 | | 5 | Carol | 786 | | 4 | David | 456 | | 3 | | 450 | +------------+--------------+----------------+ 5 rows in set (0.04 sec)
广告