我在SQL中遇到问题,我没有找到解决方案。 (对不起表格格式,这是我的第一个问题,我还没有得到如何做到这一点......)我有两张桌子:
TAB1
ID | Time | Time20Min
---|------ |---------------
1 | 2:20 | 2:40
2 | 5:13 | 5:33
3 | 1:34 | 1:54
TAB2
ID | DateTime
--- |----
1 | 2:20
1 | 2:32
1 | 2:39
1 | 2:45
2 | 5:17
2 | 5:23
2 | 5:33
2 | 5:42
我想为每个ID保留Tab2.DateTime介于Tab1.Time和Tab1.Time20Min之间的行,并且具有:
TAB3
ID | DateTime
--- |----
1 | 2:20
1 | 2:32
1 | 2:39
2 | 5:17
2 | 5:23
2 | 5:33
你能帮帮我吗?
谢谢: - )
答案 0 :(得分:1)
使用JOIN
select t2.* -- the t2 limits the results to this table only
from Tab1 t1 -- Tab1 is now aliased as t1
inner join Tab2 t2 -- JOIN to Tab2 and alias as t2
on t2.DateTime between t1.Time and T1.Time20Min -- here is the JOIN condition