table1
row_id row_one row_two
1 1 5
2 1 5
3 2 5
4 2 5
5 2 6
table2
row2_id row2_one row2_two
1 1 somevalue
2 2 somevalue2
"select distinct row_one from table1 where row_two=5"
结果
row_one
1
2
之后我想要选择
select * from table2 where row2_one=1
select * from table2 where row2_one=2
我希望选择一个查询。 我正在尝试此查询
select * from table2 where row2_one in (select distinct row_one from table1 where
row_two where row_two=5)
但花了8秒
Showing rows 0 - 14 ( 15 total, Query took 8.3255 sec)
为什么这么慢。我想要选择更快。 请帮帮我!
答案 0 :(得分:1)
您不需要DISTINCT
。你可以这样做:
SELECT *
FROM table2
WHERE row2_one IN (SELECT row_one FROM table1 WHERE row_two=5)
使用EXISTS
可能会更快:
SELECT *
FROM table2 A
WHERE EXISTS (SELECT * FROM table1 WHERE row_two=5 AND row_one = A.row2_one)
答案 1 :(得分:0)
答案 2 :(得分:0)
假设这是您的查询:
select *
from table2 where row2_one in (select distinct row_one from table1 where row_two=5)
然后这是良好的形式。有一件事,你不需要子查询中的distinct。
如果在row_two列上为table1添加索引,则应该会获得更好的性能。 table2中row2_one的索引也会加快它的速度。
答案 3 :(得分:-1)
Select distinct table2.*
from table1 t1, table2 t2
where t1.row_two =5 and t1.row2_one = t2.row2_one