我正在运行以下查询..
Select T1.* from T1
LEFT JOIN T2 ON T1.C1 = T2.C1
LEFT JOIN T3 ON T2.C3 = T3.C3
ORDER BY Case When T1.C1 = 'ABC' THEN 'XYZ' END AS COMMENTS
它在AS附近给我一个语法错误。无法弄清楚错误。
答案 0 :(得分:4)
在按
的顺序使用案例时,您不需要AS
别名
Select T1.* from T1
LEFT JOIN T2 ON T1.C1 = T2.C1
LEFT JOIN T3 ON T2.C3 = T3.C3
ORDER BY Case When T1.C1 = 'ABC' THEN 'XYZ' END
答案 1 :(得分:3)
这是因为你试图为ORDER BY子句分配一个别名 - 这是不允许的,我不确定你想在这里实现什么?
您需要:
Select T1.* from T1
LEFT JOIN T2 ON T1.C1 = T2.C1
LEFT JOIN T3 ON T2.C3 = T3.C3
ORDER BY Case When T1.C1 = 'ABC' THEN 'XYZ' END
或者,如果您希望在SELECT中返回CASE值,则需要:
Select T1.*, Case When T1.C1 = 'ABC' THEN 'XYZ' END AS COMMENTS
from T1
LEFT JOIN T2 ON T1.C1 = T2.C1
LEFT JOIN T3 ON T2.C3 = T3.C3
ORDER BY Case When T1.C1 = 'ABC' THEN 'XYZ' END
此外,再次不确定这里的意图,所以它取决于,但你可能需要一个ELSE在那个案例... e.g。
ORDER BY CASE WHEN T1.C1 = 'ABC' THEN 'XYZ' ELSE T1.C1 END