关于 order by 子句里面用到 case when 的例子
SELECT * FROM (
SELECT 'a' AS a , 1 AS b FROM dual
UNION ALL
SELECT 'a' AS a , 2 AS b FROM dual
UNION ALL
SELECT 'b' AS a , 2 AS b FROM dual
UNION ALL
SELECT 'b' AS a , 1 AS b FROM dual
) a
ORDER BY (CASE WHEN a.a = 'a' THEN 't' ELSE 'd' END ) + a.b
这里很奇怪,去掉红色的部分就可以正常运行。
此例应该如何改才能排序出
b,1
b,2
a,1
a,2
1 要求在order by 子句里面用到 case when
2 除了 order by a desc , b 这个答案。
------最佳解决方案--------------------'a'+1 当然报错
+a.b改成
------其他解决方案--------------------字符和数字怎么相加!也没意义吧,改成数字就可以了!
SELECT * FROM (
SELECT 'a' AS a , 1 AS b FROM dual
UNION ALL
SELECT 'a' AS a , 2 AS b FROM dual
UNION ALL
SELECT 'b' AS a , 2 AS b FROM dual
UNION ALL
SELECT 'b' AS a , 1 AS b FROM dual
) a
ORDER BY (CASE WHEN a.a = 'a' THEN 2 ELSE 1 END)+a.b
------其他解决方案--------------------以上的代码,在报 ora-01722 的错误。
------其他解决方案--------------------a.b