gpt4 book ai didi

sql - CASE inside ORDER BY 多列

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:53 24 4
gpt4 key购买 nike

我想有条件地订购查询

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" does not exist

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

这会在 ","处引发 语法错误

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

最佳答案

您需要使用 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

关于sql - CASE inside ORDER BY 多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39873677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com