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:对示例表感到抱歉

4

1 回答 1

1

要确定优先级a,然后b,然后time,使用order by b, a, time.

您可以使用not exists子查询仅选择每辆车的最佳行。

最后,您可以Pos使用 MySQL 的变量添加一列,例如@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

在 SQL Fiddle 上查看它。

于 2013-09-09T14:41:14.617 回答