0

我有两张桌子

Table A: a_id,timemarker (datetime)

Table B: b_id,start (datetime), stop(datetime), cat(varchar)

table A

149|2010-07-19 07:43:45

150|2010-07-19 08:01:34

151|2010-07-19 07:49:12

table B

565447|2010-07-19 07:30:00|2010-07-19 08:00:00

565448|2010-07-19 08:00:00|2010-07-19 08:20:00

我想从表 A 中选择表 B 范围内的所有行

谢谢

4

2 回答 2

0
SELECT a.* FROM a
INNER JOIN b ON
  a.timemarker BETWEEN b.start AND b.end
GROUP BY a.id 

我认为这应该更便宜。当然,额外的索引也会有所帮助:)

于 2010-12-13T16:25:30.810 回答
0

选择 ANY [B.start, B.end] 内的任何 A

select a.*
from
table a 
where exists ( select * from table b where a.timemarker between b.start and b.stop)
;

OP写道

我的钥匙有问题!查询执行时间很长。我在表 a 中有超过 40k 行,在表 b 中有超过 140 万行......表中没有任何关系 – Norman 3 秒前

是的,因为您可能会将每个 A 与每个 B = 40k * 1.4M 比较进行比较。

但你的问题是“我该怎么做”,而不是“我是这样做的,我怎样才能让它更快”。

如果你想要它更快,你需要在 B(start, end) 上添加一个索引;

于 2010-08-05T19:35:33.380 回答