我正在试图找出如何编写一个MySQL查询,该查询将返回最接近的数据,其中Actor = 210,条件为E_id = 3.
这是我的原始表:
Session Player E_id Time Actor PosX PosY index
------------------- ------ ---- ----------------------- ----- ---- ---- -----
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 210 906 466 6
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 210 342 540 7
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 31 812 244 8
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 210 248 614 9
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 21 342 688 10
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 49 812 170 11
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 50 248 466 12
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 210 718 318 13
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 52 154 466 14
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 211 499 250 15
23131885ccc560bb6c8 10125 3 01-11-2012 08:56:40.63 208 510 414 16
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 210 248 466 17
23131885ccc560bb6c8 10125 15 01-11-2012 08:56:38.323 20 718 318 18
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 52 154 466 19
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 11 499 250 20
23131885ccc560bb6c8 10125 3 01-11-2012 08:56:40.63 208 510 414 21
如果我解除查询
select * from table where E_id = 3 or Actor = 210;
我得到了这个结果
Session Player E_id Time Actor PosX PosY index
------------------- ------ ---- ----------------------- ----- ---- ---- -----
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 210 906 466 6
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 210 342 540 7
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 210 248 614 9
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 210 718 318 13
23131885ccc560bb6c8 10125 3 01-11-2012 08:56:40.63 208 510 414 16
23131885ccc560bb6c8 10125 17 01-11-2012 08:56:38.323 210 248 466 17
23131885ccc560bb6c8 10125 3 01-11-2012 08:56:40.63 208 510 414 21
预期结果为: 行索引no 16和16的索引号为13的行 行索引号为21的索引号为17的行
指数16和21 e_id 3
答案 0 :(得分:0)
您的输入是E_ID,索引和演员。索引似乎是一个唯一的列,因此您不需要E_ID。
此查询将找到最接近的(按索引距离)。但它只会查找索引较低的行(向上)。
select *
from your_tbl
where 16 - `index` >= 0 and Actor = 210
order by 16 - `index`
limit 1
16给你13和21给17
并且还有另一个查询没有仅查找的限制。它会找到最接近的索引:
select *
from your_tbl
where Actor = 210
order by abs(21 - `index`)
limit 1
我提出了两个问题,因为我不明白问题中最近的意思。如果它在索引距离abs(index1 - index2)方面最接近,那么对于16 17来说是好的结果,因为它们都有Actor = 210并且它们是相邻的
答案 1 :(得分:0)
这是一个非动态的答案。这意味着使用union
,这不是最好的。 (归功于sqlfiddle的eggyal)
查询:
select session, player, e_id,
time, actor, max(`index`)
from your_tbl
where Actor = 210
union
select session, player, e_id,
time, actor, `index`
from your_tbl
where e_id = 3
;
| SESSION | PLAYER | E_ID | TIME | ACTOR | MAX(`INDEX`) |
------------------------------------------------------------------------------------------
| 23131885ccc560bb6c8 | 10125 | 17 | January, 11 2012 08:56:38 | 210 | 17 |
| 23131885ccc560bb6c8 | 10125 | 3 | January, 11 2012 08:56:40 | 208 | 16 |
| 23131885ccc560bb6c8 | 10125 | 3 | January, 11 2012 08:56:40 | 208 | 21 |
答案 2 :(得分:0)
This is the test that it works
我真的不知道第三个或声明是否真的需要,但如果您的应用程序可能将其用作特殊情况,您可以根据需要将其删除。如果您要删除我使用的额外行,而不是选择* 在变量赋值之后,请将其更改为:
select `Session`, `Player`, `E_id`, `Time`, `Actor`, `PosX`, `PosY`, `index` ...
我没有包括那个,因为在PHP中并不总是欢迎[`]
set @i = 0;
set @j = 0;
set @k = 0;
set @l = 0;
select * from (select *, @i:=@i+1 as myrow from your_tbl where E_id = 3 or Actor=210) as tb1
where myrow in
(select myrow2-1 from
(select *, @j:=@j+1 as myrow2 from your_tbl where E_id = 3 or Actor=210) as tb2
where E_id=3) or myrow in
(select myrow3+1 from
(select *, @k:=@k+1 as myrow3 from your_tbl where E_id = 3 or Actor=210) as tb3
where E_id=3) or myrow in
(select myrow4 from
(select *, @l:=@l+1 as myrow4 from your_tbl where E_id = 3 or Actor=210) as tb4
where E_id=3)
答案 3 :(得分:-1)
我通过@eggyal示例Solutution得到我的答案,当我得到最终查询时,这很简单