【发布时间】:2020-06-30 16:29:06
【问题描述】:
我有三个表,table3 基本上是 table1 和 table2 的中间表。当我执行包含“in”并连接 table1 和 table3 的查询语句时,它一直在运行,我无法得到结果。如果我使用id=134 而不是id in (134,267,390,4234 ... ),就会出现结果。我不明白为什么“in”有效果,有人知道吗?
查询语句:
select count(*) from table1, table3 on id=table3.table1_id where table3.table2_id = 123 and id in (134,267,390,4234) and item = 30;
表结构:
table1:
id integer primary key,
item integer
table2:
id integer,
item integer
table3:
table1_id integer,
table2_id integer
-- the DB without index was 0.8 TB after the three indices is now 2.5 TB
indices on: table1.item, table3.table1_id, table3.table2_id
环境:Linux,sqlite 3.7.17
【问题讨论】:
-
您的查询无法编译。
Error: no such column: table3.table1.id我猜你的意思是table3.table1_id。 -
FROM 子句应该是
from table1 inner join table3 on table1.id = table3.table1_id或带有别名:from table1 t1 inner join table3 t3 on t1.id = t3.table1_id -
@OlivierJacot-Descombes 虽然我同意最好明确加入,
from table1, table3 on id=table3.table1_id和from table1 inner join table3 on id=table3.table1.id在 SQLite 中是等效的。见Side note: Special handling of CROSS JOIN. -
@Schwern 哦,不好意思,查询语句错了,我修改了