MySql:ORDER BY ASC,底部为空

时间:2014-09-28 05:08:17

标签: php mysql sorting

注意我的值实际上是空字符串非空。

我想在最后将表格Album ASC与空值排序。我找到的每个堆栈溢出帖子都无法在我的数据库上运行。

以下不起作用:

  • SELECT Album FROM songs ORDER BY isnull(Album,'1')
  • 从歌曲中选择专辑ORDER BY专辑NULLS LAST
  • SELECT Album FROM songs ORDER BY Album IS NULL
  • SELECT Album FROM songs ORDER BY -Album DESC
  • SELECT Album FROM songs ORDER BY ISNULL(专辑),专辑ASC
  • 从歌曲中选择专辑ORDER BY ISNULL(专辑)
  • 选择相册FROM歌曲ORDER BY(相册为空,然后1 ELSE 0 END),相册
  • 从歌曲中选择专辑ORDER BY ISNULL(专辑),专辑;
  • SELECT Album FROM songs ORDER BY case当Album为null然后2 else 1 end,Album

3 个答案:

答案 0 :(得分:1)

select album from songs ORDER BY CASE WHEN album IS NULL THEN 1 WHEN album= '' THEN 2 ELSE 3 END DESC, album ASC

它还会处理空字符串

答案 1 :(得分:1)

看看你的输出,我相信你会混淆null和空字符串。

Select case when album is null then 'NULL'
        when album = '' then 'string.empty'
        Else album end as Album
From song
Order by case when album is null then 2
        when album = '' then 1
        Else 0 end, album

http://sqlfiddle.com/#!2/84541/3

答案 2 :(得分:0)

也许考虑为PHP的ORM?学说或推动,有很多种口味。 还在手工创建SQL,为什么?

但你的NULL问题确实没有用ORM解决。

为什么不在此列上设置约束:

NOT NULL DEFAULT ''