我有两个表,我可以完美地分别查询。
table1
存储范围:
SELECT range_id, range_from, range_to FROM table1;
table2
存储 ID:
SELECT MAX(id) FROM table2 WHERE id BETWEEN <x> AND <y>;
我需要一个查询,对于 table1
中的每个元组(range_id
、from
、to
)选择
,使得 table2
中的 MAX(id)MAX(id)
位于 from 和 to 之间:
range_id max(id)
-------- -------
foo 3
bar 17
snafu 823467
我不知道如何实现这一点,因为这里没有什么可以加入的。
有什么想法吗?
最佳答案
select *
, (
select max(t2.id)
from table2 t2
where t2.id between t1.range_from and t1.range_to
) as max_id_in_range
from table1 t1
关于sql - 根据没有 JOIN 的第二个表从一个表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14235930/