在一个查询中比较两个不同的查询输出

时间:2014-08-14 05:46:22

标签: mysql sql sqlite

我有两个相互连接的表主键 - 外键。 我想检查他们的输出是否匹配。

  1. 查询::
  2. Select LINK_ID as A1, PHYSICAL_NUM_LANES as count1 from RDF_NAV_LINK where PHYSICAL_NUM_LANES is not null

    RecNo    | A1           | count1
    -----------------------------------
    1        |51364636      |2
    2        |51366793      |1
    3        |51366795      |1
    4        |51366796      |4
    
    1. 查询::
    2. Select LINK_ID as A2, count(*) as count2 from RDF_LANE Group by LINK_ID

      RecNo   |A2         |count2
      ----------------------------------
      1       |51364636   |2
      2       |51366793   |2
      3       |53676455   |3
      4       |53676460   |1
      

      现在我想检查 A1 = A2然后count1必须等于count2 (如果不是我的输出)

2 个答案:

答案 0 :(得分:1)

加入A1 = A2 AND count1 <> count2上的选择

SELECT * FROM
(Select LINK_ID as A1, PHYSICAL_NUM_LANES as count1 from RDF_NAV_LINK where PHYSICAL_NUM_LANES is not null) X
INNER JOIN
(Select LINK_ID as A2, count(*) as count2 from RDF_LANE Group by LINK_ID) Y
ON A1 = A2 AND count1 <> count2;

答案 1 :(得分:0)

试试这个

SELECT *
FROM
(
  (
   Select LINK_ID AS A1, PHYSICAL_NUM_LANES AS Count1 
   FROM RDF_NAV_LINK 
   WHERE PHYSICAL_NUM_LANES IS NOT NULL
  )S JOIN
  (
   Select LINK_ID AS A2, Count(*) As Count2 
   FROM RDF_LANE 
   GROUP BY LINK_ID
  )T ON T.A1 = S.A1 AND T.Count2 <> S.Count1
) Tmp