简体   繁体   English

如何从联接查询中获取一列的DISINCT值

[英]How to get DISINCT values of one column from a join query

I am having a query like 我正在查询

SELECT DISTINCT table_1.id_1,
    table_1.id_2,
    table_1_id_3
FROM table_1
INNER JOIN table_2
    ON table_1.id_1 = table_2.id_1
        AND table_1.id_2 = table_2.id_2
INNER JOIN table_3
    ON table_2.id_1 = table_3.id_2
        AND table_2.id_2 = table_3.id_2
WHERE table_2.code = 'Y'
    AND table_2.site = 'N'
    AND table_3.code = 'Q'

This gives me distinct combination of id_1, id_2 and id_3 satisfying the conditions. 这给了我满足条件的id_1,id_2和id_3的独特组合。 How should the query be to get distinct values of only id_1 matching all the conditions. 查询应如何获取仅满足所有条件的id_1的不同值。

I am not posting the table contents as they are having large number of columns and as this question is a query specific one. 我不发布表内容,因为它们具有大量列,并且此问题是查询特定的。

You could remove the second and third fields from the query. 您可以从查询中删除第二和第三字段。

SELECT DISTINCT 
    table_1.id_1    
FROM table_1
INNER JOIN table_2
    ON table_1.id_1 = table_2.id_1
        AND table_1.id_2 = table_2.id_2
INNER JOIN table_3
    ON table_2.id_1 = table_3.id_2
        AND table_2.id_2 = table_3.id_2
WHERE table_2.code = 'Y'
    AND table_2.site = 'N'
    AND table_3.code = 'Q'

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

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