简体   繁体   English

合并两个选择查询

[英]Merge of two select queries

I want to merge these two Select queries into single query. 我想将这两个Select查询合并到单个查询中。 How can i do this? 我怎样才能做到这一点?

 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

One solution would be to 一种解决方案是

  • add the COUNT statement as a subquery COUNT语句添加为子查询
  • (LEFT) JOIN this subselect with the SLink table (LEFT) JOIN使用SLink(LEFT) JOIN此子选择
  • Add the LinkCount to the list of selected values. LinkCount添加到所选值的列表。

SQL Statement 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                  

You might want to read up on Subqueries in the reference manual 您可能需要阅读参考手册中的“子查询”

12.2.9.8. 12.2.9.8。 Subqueries in the FROM Clause FROM子句中的子查询

Subqueries are legal in a SELECT statement's FROM clause. 子查询在SELECT语句的FROM子句中是合法的。 The actual syntax is: 实际语法是:

SELECT ... FROM (subquery) [AS] name ...

The [AS] name clause is mandatory, because every table in a FROM clause must have a name. [AS] name子句是强制性的,因为FROM子句中的每个表都必须有一个名称。 Any columns in the subquery select list must have unique names. 子查询选择列表中的任何列都必须具有唯一的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM