我想把这两个Select查询合并成一个查询。我该怎么做?
SELECT link_id, COUNT(*) FROM Loc GROUP BY link_id
SELECT Slink.[xlink:Show],Loc.[xlink:show],
Slink.[xlink:actuate],Loc.[xlink:actuate] ,
href, Sem.SemRoleDescrip
FROM Sem JOIN Loc ON
Sem.SemRoleId = Loc.SemRoleId
JOIN Slink ON Slink.link_id = Loc.link_id
最佳答案
一个解决办法是
将COUNT
语句添加为子查询(LEFT) JOIN
此子选择带有SLink
表
将LinkCount
添加到选定值列表中。
SQL语句
SELECT Slink.[xlink:Show]
, Loc.[xlink:show]
, Slink.[xlink:actuate]
, Loc.[xlink:actuate]
, href
, Sem.SemRoleDescrip
, SLinkCount.LinkCount
FROM Sem
JOIN Loc ON Sem.SemRoleId = Loc.SemRoleId
JOIN Slink ON Slink.link_id = Loc.link_id
LEFT JOIN (
SELECT link_id, COUNT(*) AS LinkCount
FROM Loc
GROUP BY
link_id
) SLinkCount ON SLinkCount.link_id = Slink.link_id
您可能需要阅读reference manual中的子查询
12.2.9.8条。FROM子句中的子查询
子查询在
SELECT
语句的FROM
子句中是合法的。实际的语法为:
SELECT ... FROM (subquery) [AS] name ...
name子句是必需的,因为a
[AS]
子句必须有名称。子查询选择列表中的任何列都必须
有唯一的名字。