不确定之前是否曾询问过但不确定要查找的内容。所以这就是问题所在:
MySQL表包含字段
A, B, C, D
A is 1 or 0
B is 1 or 0
C is 1, 2, 3 or 4
D is a date
到目前为止,B被忽略了,'order by'是:
A DESC, C DESC, D ASC
现在需要的是
A DESC, B "1 every other row else 0", C DESC, D ASC
所以我们会得到像
这样的行[A, 1, C, D]
[A, 0, C, D]
[A, 1, C, D]
[A, 0, C, D]
[A, 1, C, D]
[A, 0, C, D]
[A, 1, C, D]
[A, 0, C, D] // last 0
[A, 1, C, D]
[A, 1, C, D]
[A, 1, C, D]
知道如何在SQL中执行此操作吗?它是关于选择具有此顺序的行,因此SELECT ... ORDER BY ...
谢谢!
答案 0 :(得分:0)
需要CONFIG_FILE=/usr/cygnus/conf/agent_<id>.conf
:
order by
以下是一个完整的演示。
order by
A DESC,
IF(B = 1, 2*(@odd := @odd + 1), 2*(@even := @even + 1) + 1),
C DESC,
D ASC;
-- data
create table t1(A INT, B INT, C INT, D date);
insert into t1 values
(1,1,1,'2016-01-01'),
(1,1,2,'2016-01-02'),
(1,1,3,'2016-01-03'),
(1,1,4,'2016-01-04'),
(1,1,1,'2016-01-04'),
(1,1,2,'2016-01-05'),
(1,1,3,'2016-01-06'),
(1,0,4,'2016-01-07'),
(1,0,1,'2016-01-08'),
(1,0,2,'2016-01-09');
select * from t1 order by A DESC, C DESC, D ASC;
-- query wanted
select t1.*
from t1 CROSS JOIN (select @even := 0, @odd := 0) param
order by
A DESC,
IF(B = 1, 2*(@odd := @odd + 1), 2*(@even := @even + 1) + 1),
C DESC,
D ASC;