Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从左表中选择与右表中的行相关的行_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 从左表中选择与右表中的行相关的行

Sql 从左表中选择与右表中的行相关的行,sql,sql-server,tsql,Sql,Sql Server,Tsql,表一: I1 I2 I3 I4 第二个表C: C1 C2 C3 第三个词: I1 C2 I1 C3 I2 C2 I3 C2 I3 C3 I4 C2 I4 C3 任何只给出第一个表中所有表公用的行的SQL请求的想法,如以下结果: C2 对于另一个这样的集合: I1 C2 I1 C3 I2 C2 I3 C3 I4 C2 I4 C3 select Col2 from ( select Col2 , count(Col1) As Expr1 from CI group by Col2 ) A

表一:

I1
I2
I3
I4
第二个表C:

C1
C2
C3
第三个词:

I1 C2
I1 C3
I2 C2
I3 C2
I3 C3
I4 C2
I4 C3
任何只给出第一个表中所有表公用的行的SQL请求的想法,如以下结果:

C2
对于另一个这样的集合:

I1 C2
I1 C3
I2 C2
I3 C3
I4 C2
I4 C3
select Col2 from  ( select Col2 , count(Col1) As Expr1 from CI group by Col2 ) A
where Expr1 = (select count(*) from I) 
结果将是空的。 以及

鲁伊斯特:空的

*见讨论

谢谢。

您可以使用: 如果存在(子查询)

您可以使用:
WHERE EXISTS(子查询)

您可以使用
NOT EXISTS

SELECT * 
FROM TableC c
WHERE NOT EXISTS (  SELECT 1 
                    FROM TableI i 
                    WHERE NOT EXISTS (  SELECT 1 FROM TableCI ci 
                                        WHERE   ci.I = i.I 
                                                AND ci.C = c.C
                                     )
                )

或者,按照jarlh的建议,将[DISTINCT]归为一组

DECLARE @NumberI int = (SELECT count(*) FROM TableI)

SELECT c.C
FROM TableC c
INNER JOIN TableCI ci ON c.C = ci.C
INNER JOIN TableI i ON ci.I = i.I 
GROUP BY c.C
HAVING COUNT(DISTINCT ci.I) = @NumberI

您可以使用
不存在

SELECT * 
FROM TableC c
WHERE NOT EXISTS (  SELECT 1 
                    FROM TableI i 
                    WHERE NOT EXISTS (  SELECT 1 FROM TableCI ci 
                                        WHERE   ci.I = i.I 
                                                AND ci.C = c.C
                                     )
                )

或者,按照jarlh的建议,将[DISTINCT]归为一组

DECLARE @NumberI int = (SELECT count(*) FROM TableI)

SELECT c.C
FROM TableC c
INNER JOIN TableCI ci ON c.C = ci.C
INNER JOIN TableI i ON ci.I = i.I 
GROUP BY c.C
HAVING COUNT(DISTINCT ci.I) = @NumberI

因此,仅当表CI的第二列中的值与表I中存在的所有记录相关联时,才显示表CI的第二列

如果是这样,您可以这样做:

I1 C2
I1 C3
I2 C2
I3 C3
I4 C2
I4 C3
select Col2 from  ( select Col2 , count(Col1) As Expr1 from CI group by Col2 ) A
where Expr1 = (select count(*) from I) 

因此,仅当表CI的第二列中的值与表I中存在的所有记录相关联时,才显示表CI的第二列

如果是这样,您可以这样做:

I1 C2
I1 C3
I2 C2
I3 C3
I4 C2
I4 C3
select Col2 from  ( select Col2 , count(Col1) As Expr1 from CI group by Col2 ) A
where Expr1 = (select count(*) from I) 

这是一个简单的解决方案,其基本前提是:
我们只需要那些C记录,对于这些C记录,其I记录的Count Distinct应该与整个表的I记录的Count Distinct相同

            select TBL.C, count(distinct TBL.I)
            from TBL
            group by TBL.C
            having count(distinct TBL.I)= (select count(distinct TBL.I) from TBL)
在Oracle中,以下是查询和结果集

           with TBL 
            as
            (
            select 'I1' I,'C2' C from dual
            union all
            select 'I1','C3' from dual
            union all
            select 'I2','C2' from dual
            union all
            select 'I3','C2' from dual
            union all
            select 'I3','C3' from dual
            union all 
            select 'I4','C2' from dual
            union all
            select 'I4','C3' from dual
            )

            select TBL.C, count(distinct TBL.I)
            from TBL
            group by TBL.C
            having count(distinct TBL.I)= (select count(distinct TBL.I) from TBL)
结果集:

    C2   4

这是一个简单的解决方案,其基本前提是:
我们只需要那些C记录,对于这些C记录,其I记录的Count Distinct应该与整个表的I记录的Count Distinct相同

            select TBL.C, count(distinct TBL.I)
            from TBL
            group by TBL.C
            having count(distinct TBL.I)= (select count(distinct TBL.I) from TBL)
在Oracle中,以下是查询和结果集

           with TBL 
            as
            (
            select 'I1' I,'C2' C from dual
            union all
            select 'I1','C3' from dual
            union all
            select 'I2','C2' from dual
            union all
            select 'I3','C2' from dual
            union all
            select 'I3','C3' from dual
            union all 
            select 'I4','C2' from dual
            union all
            select 'I4','C3' from dual
            )

            select TBL.C, count(distinct TBL.I)
            from TBL
            group by TBL.C
            having count(distinct TBL.I)= (select count(distinct TBL.I) from TBL)
结果集:

    C2   4

GROUP BY,HAVING,COUNT[DISTINCT]GROUP BY,HAVING,COUNT[DISTINCT]我有
内部联接表I
来检查ci。我必须在表中。第二个解决方案似乎工作正常,除非第一个表的一个元素与第二个表中的任何元素都没有关联,例如:I5 NULL在这种情况下如何
计数(DISTINCT ci.I)=@NumberI
?它将不存在于结果集中。它将不存在于表IC中,但存在于表I中,且不与表C的任何元素关联,由于这个原因,结果将是空的。我有
内部联接表I
来检查ci。我必须在表中。第二个解决方案似乎可以正常工作,除非第一个表的一个元素与第二个表中的任何元素都没有关联,例如:I5 null在这种情况下如何
计数(不同的ci.I)=@NumberI
?它将不存在于结果集中。它将不存在于表IC中,但存在于表I中,且不与表C的任何元素关联,因此结果将为空。