1

我正在尝试完成这段代码:

ORDER BY IF(j.groups IS NULL OR j.groups = '',  IF(j.title IS NULL, i.title), j.groups)

英文:如果存在则首先按 j.groups 排序,如果存在则按 j.title 排序,最后按 i.title 排序。但以上行不通。

4

2 回答 2

1

试试这个:

ORDER BY IF(j.groups IS NULL OR j.groups = '', IFNULL(j.title, i.title), j.groups);

或者

ORDER BY COALESCE(j.groups, j.title, i.title);

或者

ORDER BY IF(j.groups IS NULL OR j.groups = '', IF(j.title IS NULL, i.title, 1), j.groups);
于 2013-01-04T05:02:21.750 回答
0

你忘记了你内在的else部分IF。试试这个:

ORDER BY IF(
  j.groups IS NULL OR j.groups = '',
  IF(
    j.title IS NULL,
    i.title,
    j.title -- this was missing
  ),
  j.groups
)

但我发现这种语法更具可读性:

ORDER BY COALESCE(IF(j.groups = '', NULL, j.groups), j.title, i.title)
于 2013-01-04T05:04:58.777 回答