select a.c3 from (select c2,c3 from a where c1=1) a where a.c2 in (select c2,c3 from a where c1=2) b

解决方案 »

  1.   

    我发昏了语句应该是
    select b.c3 from (select c2,c3 from a where c1=1) b where b.c2 in (select c2,c3 from a where c1=2) 
      

  2.   

    SELECT A.column3
    FROM
    (select column1,column2,column3 from table1 where column1=1) A,(select column1,column2,column3 from table1 where column1=2) B
    WHERE A.column2=B.column2
      

  3.   

    十分感谢两位高手的回答,但我觉得可能还存在点问题,不知是不是我没有理解正确。
    对于guo的语句:
     我想可能是忽略了其实s3并不是s1中的某一部分或s2的某一部分,而guo的语句显然只是s1中的一部分,反过来讲,将sql语句换成:
    select b.c3 from (select c2,c3 from a where c1=2) b where b.c2 in (select c2,c3 from a where c1=1) 
    其结果集就不一样了。
    而DeD的语句,我想是不是应该这样:
    SELECT A.column3,b.column3
    FROM
    (select column1,column2,column3 from table1 where column1=1) A,(select column1,column2,column3 from table1 where column1=2) B
    WHERE A.column2=B.column2 我不敢确定,请大家讨论。
      

  4.   

    试试这个,这里col就是column的缩写
    select a.*
    from (select * from tab1 where col1='1') a,  (select * from tab1 where col1='2') b where a.col2=b.col2 and a.col3=b.col3
      

  5.   

    试试这个,这里col就是column的缩写
    select a.*
    from (select * from tab1 where col1='1') a,  (select * from tab1 where col1='2') b where a.col2=b.col2 and a.col3=b.col3
      

  6.   

    如果:
    1.column1=1时的集合s1
    2.column1=2时的集合s2

    3.求当s1和s2的column2相等时的交集s3 -->> 空集
      

  7.   

    比如下面的数据:col1 col2 col3 
    1     2     9
    2     3     7
    2     2     20那你要返回哪一个值呢?
    9,还是20,还是两个都要?
      

  8.   

    怎么可能是空集呢,前提是column1和column2是主键。
    可能是我没说清楚,并不是s1和s2的交集,
    而是s1和s2的column2相等时的集合s3,不好意思啊。
    答案应该是:
    SELECT A.column3,b.column3
    FROM
    (select column1,column2,column3 from table1 where column1=1) A,(select column1,column2,column3 from table1 where column1=2) B
    WHERE A.column2=B.column2 
      

  9.   

    我觉得逻辑有问题,不一定S3是空集,但S3的记录不能确定,因为当S1.COLUMN2=S2.COLUMN2时,s1.column3可能不等于s2.column3。
      

  10.   

    我觉得逻辑有问题,不一定S3是空集,但S3的记录不能确定,因为当S1.COLUMN2=S2.COLUMN2时,s1.column3可能不等于s2.column3。