将SQL查询的结果与另一个结果相关联

时间:2018-06-13 12:23:00

标签: sql

我有这个查询

SELECT BRAND.NAME, FIXTURE.NAME AS FIXTURE_NAME, FIXTURECATEGORY.NAME AS FIXTURE_CATEGORY
FROM            V_APPROVED_FIXTURES FIXTURE INNER JOIN
                         BRAND ON FIXTURE.BRAND_ID = BRAND.ID INNER JOIN
                         FIXTURECATEGORY ON FIXTURE.FIXTURECATEGORY_ID = FIXTURECATEGORY.ID INNER JOIN
                         FIXTURE_MODE ON FIXTURE.ID = FIXTURE_MODE.FIXTURE_ID AND FIXTURE.VERSION = FIXTURE_MODE.FIXTURE_VERSION RIGHT JOIN
                         FIXTURE_MODE_PARAMETER ON FIXTURE_MODE.ID = FIXTURE_MODE_PARAMETER.FIXTURE_MODE_ID AND FIXTURE_MODE.FIXTURE_ID = FIXTURE_MODE_PARAMETER.FIXTURE_ID AND 
                         FIXTURE_MODE.FIXTURE_VERSION = FIXTURE_MODE_PARAMETER.FIXTURE_VERSION

GROUP BY BRAND.NAME, FIXTURE.NAME, FIXTURECATEGORY.NAME
HAVING        COUNT(FIXTURE_MODE_PARAMETER.ORDINAL)=25

生成一个唯一结果列表,我想将它与此

结合起来
SELECT BRAND.NAME, FIXTURE.NAME AS FIXTURE_NAME, FIXTURECATEGORY.NAME AS FIXTURE_CATEGORY
FROM            V_APPROVED_FIXTURES FIXTURE INNER JOIN
                         BRAND ON FIXTURE.BRAND_ID = BRAND.ID INNER JOIN
                         FIXTURECATEGORY ON FIXTURE.FIXTURECATEGORY_ID = FIXTURECATEGORY.ID INNER JOIN
                         FIXTURE_MODE ON FIXTURE.ID = FIXTURE_MODE.FIXTURE_ID AND FIXTURE.VERSION = FIXTURE_MODE.FIXTURE_VERSION RIGHT JOIN
                         FIXTURE_MODE_PARAMETER ON FIXTURE_MODE.ID = FIXTURE_MODE_PARAMETER.FIXTURE_MODE_ID AND FIXTURE_MODE.FIXTURE_ID = FIXTURE_MODE_PARAMETER.FIXTURE_ID AND 
                         FIXTURE_MODE.FIXTURE_VERSION = FIXTURE_MODE_PARAMETER.FIXTURE_VERSION
GROUP BY BRAND.NAME, FIXTURE.NAME, FIXTURECATEGORY.NAME
HAVING        COUNT(FIXTURE_MODE_PARAMETER.ORDINAL)=13

所以,我希望看到所有具有13和25 COUNT(FIXTURE_MODE_PARAMETER.ORDINAL)

的灯具

我该怎么做?

2 个答案:

答案 0 :(得分:2)

由于您的两个查询看起来相同,除了where条件。只需将orin

组合在一起
HAVING COUNT(FIXTURE_MODE_PARAMETER.ORDINAL) in(13,25)

答案 1 :(得分:0)

您可以在查询之间放置一个Union语句,如下所示:

Select * from tableA
Union
Select * from tableB

你必须把你的小组放在第二个表之后,如下所示:

Select * from tableA
Union
Select * from tableB
Group by [Column]