我对以下 SQL 查询有疑问,
如果现在是 13 下面是 SQL 查询的输出,
starts ends
11 (13)
13 (14)
11 (15)
----------
(14) 19
(15) 16
(16) 21
----------
9 (12)
8 (11)
10 (10)
如你所见,我有 3 个条件,
第一个是
13 <= end AND 13 >= start ORDER BY end ASC
然后第二个是
13 <开始 ORDER BY 开始 ASC
那么第三个是
13 > end ORDER BY end DESC
我的解决方案是执行 3 个查询,稍后在 PHP 中我会将这些结果合并为 1 个结果。还有其他方法吗?
最佳答案
您可以将多个键放在一个order by
中:
order by (case when 13 <= end and 13 >= start then 1
when 13 > start then 2
when 13 < start then 3
end),
(case when 13 <= end and 13 >= start then end end),
(case when 13 < start then start end),
(case when 13 < start then end end) desc
关于mysql - SQL Order By 带条件和括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49000760/