我有两个疑问:
Array allIds = select id from table1 order by time
和
select * from table1 where id in (allIds[0],allIds[1],...,allIds[9])
有没有办法将这些查询合并为一个?我需要来自两个查询的所有数据。
我有两个疑问:
Array allIds = select id from table1 order by time
和
select * from table1 where id in (allIds[0],allIds[1],...,allIds[9])
有没有办法将这些查询合并为一个?我需要来自两个查询的所有数据。
有两种方法可以做到:
第一种方法或多或少是您的两个查询的简单组合:
select * from table1
where id in (
select id
from table2)
order by time
第二种更好的方法是连接:
select table1.id
from table1
join table2 on table1.id = table2.id
order by time
如果我理解正确,我认为这就是您要寻找的东西。
基本上,以下查询将从第二个表中选择所有行,其中它们具有与第一个表中最近的 10 行相同的 ID。
这有意义吗?还是我错过了标记?
SELECT table.id, table2.*
FROM table
INNER LEFT JOIN table AS `table2` ON table.id IN (
SELECT table.id
FROM table
ORDER BY table.time
LIMIT 0,10
)