从ID范围获取数据并存储在单独的PHP数组中-复杂的MySQL查询
我必须做一个非常不寻常的Mysql查询 我有一个表,其中有两个列表,当前有5k行,如下所示:从ID范围获取数据并存储在单独的PHP数组中-复杂的MySQL查询,php,mysql,Php,Mysql,我必须做一个非常不寻常的Mysql查询 我有一个表,其中有两个列表,当前有5k行,如下所示: **Id** **Color** 2369 0 2370 2 2372 0 2373 1 2374 2 2375 2 等等,这都是非常基本的,但现在我必须按特定的顺序过滤掉,例如: **Color** 2 0 1 2 在这种情况下,查
**Id** **Color**
2369 0
2370 2
2372 0
2373 1
2374 2
2375 2
等等,这都是非常基本的,但现在我必须按特定的顺序过滤掉,例如:
**Color**
2
0
1
2
在这种情况下,查询应该给我Ids 2370-2374。
这些ID应该保存在php数组或单独的php变量中。不过,最好使用数组
我的问题是什么是最好的方法
对于那些可能想声称这个问题是重复的人,我不知道如何描述这个过程,所以我无法用谷歌搜索它
编辑:
示例顺序可以是任何内容:
**Color**
1. 2
2. 0
3. 1
4. 2
我想筛选前2行中的哪一行,就像您在我的第一个代码块中看到的一样,它是id为2370的行,之后是id为0的行
一行取决于它的底行和顶行
例如,如果我将我的示例中的0替换为2,则第一个代码块中的id都不会为真
<>我想检查第一行到最新的ID,如果这个命令弹出了免责声明-这是一个坏方法:D认为这可能只有当你必须手动执行或优化不是问题。p> 从您的第一个示例
2370->2372
中,我假设您的id可能会跳过一些数字,但您仍然希望匹配“下一行”,因此首先我创建了一个丑陋的子表,以获得4个连续id的id对
SELECT
t1.id as t1_id,
@sep := '-' as "id/color separator",
@color_len := 1 as "color value length",
@t2 := (SELECT CONCAT(id, @sep, color) FROM tbl WHERE id > t1.id ORDER BY id LIMIT 1) as "second row",
@t2_sep := LOCATE(@sep, @t2) as "second id length",
@t2_id := SUBSTRING(@t2, 1, @t2_sep - 1) as t2_id,
@t3 := (SELECT CONCAT(id, @sep, color) FROM tbl WHERE id > @t2_id ORDER BY id LIMIT 1) as "third row",
@t3_sep := LOCATE(@sep, @t3) as "third id length",
@t3_id := SUBSTRING(@t3, 1, @t3_sep - 1) as t3_id,
@t4 := (SELECT CONCAT(id, @sep, color) FROM tbl WHERE id > @t3_id ORDER BY id LIMIT 1) as "forth row",
@t4_sep := LOCATE(@sep, @t4) as "forth id length",
@t4_id := SUBSTRING(@t4, 1, @t4_sep - 1) as t4_id,
t1.color as t1_color,
SUBSTRING(@t2, @t2_sep + 1, @color_len) as t2_color,
SUBSTRING(@t3, @t3_sep + 1, @color_len) as t3_color,
SUBSTRING(@t4, @t4_sep + 1, @color_len) as t4_color
FROM
tbl as t1
WHERE
t1.color = 2;
然后只过滤出与所需颜色顺序匹配的行
SELECT
t.t1_id,
t.t2_id,
t.t3_id,
t.t4_id
FROM
(
/* previous query */
) as t
WHERE
t.t2_color = 0
AND
t.t3_color = 1
AND
t.t4_color = 2;
现在,如果您要规范化您的ID,使其不会在保存/删除记录或随时间重新排列时跳过数字,那么解决方案很简单:
SELECT
t1.id,
t2.id,
t3.id,
t4.id
FROM
tbl as t1
INNER JOIN tbl as t2 ON t2.id = t1.id + 1 AND t2.color = 0
INNER JOIN tbl as t3 ON t3.id = t1.id + 2 AND t3.color = 1
INNER JOIN tbl as t4 ON t4.id = t1.id + 3 AND t4.color = 2
WHERE
t1.color = 2
免责声明-这是一个坏方法:D认为这可能只有当你必须手动执行或优化是不是一个问题。p> 从您的第一个示例
2370->2372
中,我假设您的id可能会跳过一些数字,但您仍然希望匹配“下一行”,因此首先我创建了一个丑陋的子表,以获得4个连续id的id对
SELECT
t1.id as t1_id,
@sep := '-' as "id/color separator",
@color_len := 1 as "color value length",
@t2 := (SELECT CONCAT(id, @sep, color) FROM tbl WHERE id > t1.id ORDER BY id LIMIT 1) as "second row",
@t2_sep := LOCATE(@sep, @t2) as "second id length",
@t2_id := SUBSTRING(@t2, 1, @t2_sep - 1) as t2_id,
@t3 := (SELECT CONCAT(id, @sep, color) FROM tbl WHERE id > @t2_id ORDER BY id LIMIT 1) as "third row",
@t3_sep := LOCATE(@sep, @t3) as "third id length",
@t3_id := SUBSTRING(@t3, 1, @t3_sep - 1) as t3_id,
@t4 := (SELECT CONCAT(id, @sep, color) FROM tbl WHERE id > @t3_id ORDER BY id LIMIT 1) as "forth row",
@t4_sep := LOCATE(@sep, @t4) as "forth id length",
@t4_id := SUBSTRING(@t4, 1, @t4_sep - 1) as t4_id,
t1.color as t1_color,
SUBSTRING(@t2, @t2_sep + 1, @color_len) as t2_color,
SUBSTRING(@t3, @t3_sep + 1, @color_len) as t3_color,
SUBSTRING(@t4, @t4_sep + 1, @color_len) as t4_color
FROM
tbl as t1
WHERE
t1.color = 2;
然后只过滤出与所需颜色顺序匹配的行
SELECT
t.t1_id,
t.t2_id,
t.t3_id,
t.t4_id
FROM
(
/* previous query */
) as t
WHERE
t.t2_color = 0
AND
t.t3_color = 1
AND
t.t4_color = 2;
现在,如果您要规范化您的ID,使其不会在保存/删除记录或随时间重新排列时跳过数字,那么解决方案很简单:
SELECT
t1.id,
t2.id,
t3.id,
t4.id
FROM
tbl as t1
INNER JOIN tbl as t2 ON t2.id = t1.id + 1 AND t2.color = 0
INNER JOIN tbl as t3 ON t3.id = t1.id + 2 AND t3.color = 1
INNER JOIN tbl as t4 ON t4.id = t1.id + 3 AND t4.color = 2
WHERE
t1.color = 2
您希望从表中提取什么?请通过添加有关提取记录条件的详细信息来更新您的问题?我可以使用保存在变量中的Id。我使用php顺便说一句,“我必须按特定的顺序过滤掉它”->这意味着什么?是否有任何规则或只是随机选择?否顺序与第二个代码块中的顺序相似,我想筛选我的数据库,如果此顺序在某个位置,您希望从表中提取什么?请通过添加有关获取记录的条件的详细信息来更新您的问题?我可以使用保存在变量中的Id。我使用php顺便说一句,“我必须按特定的顺序过滤掉它”->这意味着什么?是否有任何规则或只是随机选择?不,顺序就像在第二个代码块中一样,如果这个顺序在某个地方,我想筛选我的数据库there@TimL. 当然,您希望使用简短的查询并规范化您的id顺序:D当您的表增长时,大型查询会变慢,“有一天”您可能希望返回并稍微优化您的数据结构:D@TimL. 当然,希望您使用简短的查询并规范化您的id顺序:D当您的表增长时,大型查询会变得缓慢,“有一天”您可能希望返回并稍微优化您的数据结构:D