SQL查询以下输出?

Gau*_*rav 2 mysql sql

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

the*_*dow 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
Run Code Online (Sandbox Code Playgroud)

(在我的测试中它给出了你列出的相同结果)