SQL SubQueries语法

时间:2014-04-09 23:55:12

标签: sql subquery

我正在处理涉及子选择查询的问题。我已经设置了数据库以使选择查询工作,但我对这些子查询相对较新并且有点迷失。我需要带有超过1张专辑的乐队名称和乐队的专辑头衔。到目前为止,这是我的代码。

SELECT b.Name, a.Title
FROM Band as b, Album as a
WHERE (SELECT * 
       FROM Album as al
       WHERE al.BID = a.BID)
GROUP BY b.Name
HAVING Count(a.BID) > 1;

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT b.Name, a.Title 
FROM Band b 
INNER JOIN Album a ON b.BID=a.BID
WHERE a.BID IN (SELECT BID FROM Album GROUP BY BID HAVING COUNT(BID)>1)

这将有效,因为Album表有一个BID作为Band表的外键