[英]MySQL all select rows in range
I need some help retrieving data from rows in range.我需要一些帮助来从范围内的行中检索数据。 Let's say this is my table:
假设这是我的桌子:
That's what I need:这就是我需要的:
I need to sort the rows by ID and then get 9 rows, where the gameID is 1 and winner's unique_id must be in the middle.我需要按 ID 对行进行排序,然后得到9行,其中gameID为1,并且获胜者的unique_id必须在中间。 For example:
例如:
$gameID = 1;
$winner = "iii";
so it should return:所以它应该返回:
5.eee
6.fff
7.ggg
8.hhh
9.iii <--- winner in the middle
10.jjj
11.kkk
12.lll
13.mmm
How can I achieve this result?我怎样才能达到这个结果?
Thanks.谢谢。
EDIT:编辑:
$b = $db->query("SELECT * FROM test WHERE gameID = 1 AND unique_id = 'iii'");
$res = $b->fetch();
$winnerID = $res['ID'];
$b2 = $db->query("SELECT * FROM test WHERE ID BETWEEN $winnerID-4 AND $winnerID+4 ORDER BY ID ASC");
$data = $b2->fetchAll();
This works, but I was wondering if it's possible in a single row.这有效,但我想知道是否可以在一行中。
Is this what you're looking for?这是你要找的吗?
It first finds the id of the winner, then selects rows with IDs smaller and greater than 4. (4 + 4 + 1 = 9)它首先找到获胜者的 id,然后选择 ID 小于和大于 4 的行。 (4 + 4 + 1 = 9)
SELECT *
FROM rows
WHERE id BETWEEN
(SELECT @id := id FROM rows WHERE unique_id = "iii") - 4 AND @id + 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.