带有多列的ORDER BY中的CASE

时间:2016-10-05 12:14:38

标签: sql database postgresql sql-order-by case

我想有条件地订购查询

minScale

但它引发了语法错误ORDER BY CASE WHEN TRUE THEN users.name, users.lastname END

下面的考试就像一个魅力

syntax error near or at ","

我也试过

ORDER BY 
CASE
WHEN TRUE THEN users.name END

引发ORDER BY CASE WHEN TRUE THEN "users.name, users.lastname" END 不存在

"users.name, users.lastname"

这会引发 ORDER BY CASE WHEN TRUE THEN "users"."name", "users"."lastname" END

如何将多个列放在ORDER BY中的CASE中?

2 个答案:

答案 0 :(得分:3)

您需要使用ROW constructor才能将几列组合在一起

ORDER BY 
CASE
WHEN TRUE THEN ROW(users.name, users.lastname) END

如果ROW()内的值超过1,则可以跳过ROW个关键字。这是带语法糖的较短版本:

ORDER BY 
CASE
WHEN TRUE THEN (users.name, users.lastname) END

答案 1 :(得分:1)

只需使用两个case表达式:

ORDER BY (CASE WHEN TRUE THEN users.name END),
         (CASE WHEN TRUE THEN users.lastname END)