表很简单:
id  |  acc
-----------
1 a
1 b
1 c
2 a
2 d
3 a
3 b需求也很简单,就是要acc里有a和c的ID,也就是把ID为1打出来:
id  |  acc
-----------
1 a
1 c
或者直接显示出来
id
1看着很简单,但是就是想不起来怎么写,试了半天就试出下面的SQL:
select a.id from (select id,acc from test where acc='a' or acc='c' ) a where a.acc='c';
但是如果要求中包含的acc的值数量不确定就死了,比如要显示acc值有a,b,c,d不知我描述的是否清楚,请各位大侠指教sql

解决方案 »

  1.   

    select * from aa where acc in('a','c');
     
            ID ACC
    ---------- ------------
             1 a
             1 c
             2 a
             3 a
      

  2.   


    我要的是'a'和'c'都必须有,不是可以有1个就行,id为2和3的都不符合要求
      

  3.   


    我要的是'a'和'c'都必须有,不是可以有1个就行,id为2和3的都不符合要求
    那你用这个吧:select id
      from (select id, acc from table where acc in ('a', 'c'))
     group by id
    having count(id) >= 2