Mysql 按多个条件订购
我很傻,这让我不知所措,这是一个词吗 排名按时间而定,但Mysql 按多个条件订购,mysql,sql,filtering,distinct,Mysql,Sql,Filtering,Distinct,我很傻,这让我不知所措,这是一个词吗 排名按时间而定,但 time done with ( A=0 ) AND ( B=0 ) beat everyone time done with ( A=0 ) AND ( B=1 ) beat everyone with ( A=1 ) time done with ( A=1 ) AND ( B=0 ) beat everyone with ( A=1 + B=1 ) 排名示例轨迹=沙漠 pos--car------time---A----B 1.-
time done with ( A=0 ) AND ( B=0 ) beat everyone
time done with ( A=0 ) AND ( B=1 ) beat everyone with ( A=1 )
time done with ( A=1 ) AND ( B=0 ) beat everyone with ( A=1 + B=1 )
排名示例轨迹=沙漠
pos--car------time---A----B
1.---yellow----90----No---No
2.---red-------95----No---No
3.---grey-----78-----No---Yes
4.---orange--253---No---Yes
5.---black----86----Yes---No
6.---white----149---Yes---No
7.---pink-----59----Yes---Yes
8.---blue-----61----Yes---Yes
更糟糕的是,该表接受同一辆车的多条记录
这是参赛作品
create table `rank`
(
`id` int not null auto_increment,
`track` varchar(25) not null,
`car` varchar(32) not null,
`time` int not null,
`a` boolean not null,
`b` boolean not null,
primary key (`id`)
);
insert into rank (track,car,time,a,b) values
('desert','red','95','0','0'),
('desert','yellow','89','0','1'),
('desert','yellow','108','0','0'),
('desert','red','57','1','1'),
('desert','orange','120','1','0'),
('desert','grey','85','0','1'),
('desert','grey','64','1','0'),
('desert','yellow','90','0','0'),
('desert','white','92','1','1'),
('desert','orange','253','0','1'),
('desert','black','86','1','0'),
('desert','yellow','94','0','1'),
('desert','white','149','1','0'),
('desert','pink','59','1','1'),
('desert','grey','78','0','1'),
('desert','blue','61','1','1'),
('desert','pink','73','1','1');
请帮忙p
ps:很抱歉,这个示例表将a、b、时间进行优先级排序,使用顺序b、a、时间
您可以使用not exists子查询仅选择每辆车的最佳行
最后,您可以使用MySQL的变量添加Pos列,如@rn:=@rn+1
查询示例:
select @rn := @rn + 1 as pos
, r.*
from rank r
join (select @rn := 0) init
where not exists
(
select *
from rank r2
where r.car = r2.car
and (
r2.a < r.a
or (r2.a = r.a and r2.b < r.b)
or (r2.a = r.a and r2.b = r.b and r2.time < r.time)
)
)
order by
b
, a
, time
要对a、b、时间进行优先级排序,请使用按b、a、时间排序
您可以使用not exists子查询仅选择每辆车的最佳行
最后,您可以使用MySQL的变量添加Pos列,如@rn:=@rn+1
查询示例:
select @rn := @rn + 1 as pos
, r.*
from rank r
join (select @rn := 0) init
where not exists
(
select *
from rank r2
where r.car = r2.car
and (
r2.a < r.a
or (r2.a = r.a and r2.b < r.b)
or (r2.a = r.a and r2.b = r.b and r2.time < r.time)
)
)
order by
b
, a
, time
这是什么意思?你只想要每辆车的顶部入口吗?是的。我试着用它来处理订单上的多个“case-when”。。但这真的很难。我永远也做不到。。排名必须有每辆车的最佳记录。。最好的记录由A过滤,BYou可以使用not exists子查询过滤每辆车的最佳行,并在答案中更新。这是什么意思?你只想要每辆车的顶部入口吗?是的。我试着用它来处理订单上的多个“case-when”。。但这真的很难。我永远也做不到。。排名必须有每辆车的最佳记录。。最佳记录由A过滤,您可以使用not exists子查询过滤每辆车的最佳行,并在答案中更新。