0

我想按字母顺序返回项目列表,但将“空”结果附加到末尾而不是出现在开头。有没有办法使用 MySQL ORDER 语句或其他方式来做到这一点?

SELECT * FROM persons WHERE status = 'active' ORDER BY lastName;

我得到什么:

Jason
Peter
Frank Asimov
Reda Banks
Scott Sorrel

我想要的是:

Frank Asimov
Reda Banks
Scott Sorrel
Jason 
Peter
4

3 回答 3

1
 SELECT * FROM persons WHERE status = 'active' 
 ORDER BY case when ifnull(lastName,'') = '' then 1 else 0 end, lastname
于 2012-11-08T19:32:35.313 回答
0

您可以使用 FIELD 语句来完成。它是这样的:

SELECT [...] ORDER BY FIELD(lastName, '') DESC

这会将所有空字符串姓氏附加到末尾。如果您的数据库中有 NULL 而不是空字符串:

SELECT [...] ORDER BY FIELD(lastName, NULL) DESC;
于 2012-11-08T19:34:29.977 回答
-1

我相信您所需要的ORDER BY只是firstName

SELECT * FROM persons WHERE status = 'active' ORDER BY lastName, firstName
于 2012-11-08T19:28:07.190 回答