表test
列compname version
和表Bugs
列bugid compname
test
的数据:
A 1.2
B 1.5
C 1.6
B 1.3
C 1.5
A 1.6
B 1.6
Bugs
的数据:
1 A
1 C
2 A
2 B
3 A
3 B
3 C
查询是:
Output the compname where version=1.6 and affected by bugid=1 along with the first(min) version in which the component appeared
输出:
A 1.2
C 1.5
我正在使用此查询,但可以加快速度:
select compname,min(version) from test where compname IN (select compname from test where version='1.6' and compname IN (select compname from Bugs where bugid=1)) group by compname
答案 0 :(得分:5)
联接速度更快,您需要额外的自我加入才能获得最低版本。
SELECT t.compname, min(t2.version)
FROM test t
INNER JOIN Bugs b
ON t.compname = b.compname
INNER JOIN test t2
ON t.compname = t2.compname
WHERE bugid = 1 AND t.version='1.6'
GROUP BY t.compname
(在我的测试中,它给出了你列出的相同结果)
答案 1 :(得分:0)
您可以使用INNER JOIN:
SELECT test.compname, min(test.version)
FROM test INNER JOIN Bugs
ON test.compname = Bugs.compname
WHERE test.version = '1.6' AND Bugs.bugid =1
GROUP BY test.compname