一个表 a (id int,  a int, b int )
例如
id  a  b
1   1   3
2   1   4 
3   1    5
4   2    1
5   2    3
6   3    3
7   3    3
8   4    3
9   5    2
。。这里的限制条件是group by  a, b
如果 在 a  b 的各个分组里 b全部为3 那么这个分组去掉。
按照这个条件上面的记录 id=5,6,7,8 的将去掉   (a b 分组b不全为3的可以保留)急切询问这个语句如何写  

解决方案 »

  1.   

    id  a  b
    5   2  3
    6   3  3
    7   3  3
    8   4  3能解释一下,为什么 8 也去掉?
      

  2.   

    8 4  3
    这个分组 里  ,a=4, b=3
    而且 仅仅该组只有1条记录 b=3
    所以去掉 
      

  3.   

    我的 语句是 
    select  *  from ..
    。group by a,b  按照a b 进行分组如果在 a b 一个分组里b的值全部为3 那么这个分组去掉
      

  4.   

    把b=3的全部去掉?
    直接在分组前加个 where b!=3,是这样吗?
      

  5.   

    现在简化sql (难度大大降低)当分组 a=3  的时候 如果该组 b的值全部为3那么这个分组去掉。当分组 a=3  的时候 如果该组 b的值不全部为3那么这个分组保留
     
    当分组a《》3 数据全部保留
      

  6.   

    4  2    1 
    5  2    3 
    这个分组 a=2  , b=1,和 3  ,这个a=2的分组里b不全部为3 所以 该组保留请注意现在难度大大减低:
    现在简化sql (难度大大降低) 当分组 a=3  的时候 如果该组 b的值全部为3那么这个分组去掉。 当分组 a=3  的时候 如果该组 b的值不全部为3那么这个分组保留 当分组a《》3 数据全部保留
      

  7.   


    当分组 a=3  的时候 如果该组 b的值全部为3那么这个分组去掉。 当分组 a=3  的时候 如果该组 b的值不全部为3那么这个分组保留 当分组a《》3 数据全部保留mysql> select *
        -> from t_liyihongcug t
        -> where a!=3
        -> or exists (select 1 from t_liyihongcug where a=t.a and b!=3);
    +------+------+------+
    | id   | a    | b    |
    +------+------+------+
    |    1 |    1 |    3 |
    |    2 |    1 |    4 |
    |    3 |    1 |    5 |
    |    4 |    2 |    1 |
    |    5 |    2 |    3 |
    |    8 |    4 |    3 |
    |    9 |    5 |    2 |
    +------+------+------+
    7 rows in set (0.00 sec)mysql>
      

  8.   

    select *
        -> from t_liyihongcug t
        -> where a!=3
        -> or exists (select 1 from t_liyihongcug where a=t.a and b!=3);感觉有问题的 
    exists (select 1 from t_liyihongcug where a=t.a and b!=3);
    (如果  有 
    a=3,b=3
    a=3 ,b=4)那么a=3 这个分组的所有数据是应该保留的哦。 ???
      

  9.   

    楼上的语句是没有问题的 确实解决了问题 
    但是select *
        -> from t_liyihongcug t
    我实际的不是一个表的  实际上是多个表组合而成的哦
    select * from (select b.disid , a. typeId from Out a, Event b
    where  a.eventId=b.id) t
    where
    t.disid != 3  or 
    exist (select 1 from t where t.disciplineid=3 and t.typeId!=47);死活编译不通过 ,可能逻辑上有问题  ??
      

  10.   

    t_liyihongcug  实际情况不是一个表
     如果他是多个表,  为了简化,现在肯定他是2个表组合而成的哦 
    如 (select * from b, c where b.id=c.id )  t 不知道如何改写这个sql
      

  11.   

    1  是这样原始问题实在过于复杂,6-7表形成1个表 
    2  另外表很不方便公开

    学会举例。你可以把表名改成 t1,t2,t3可以把字段名改成 c1,c2,c3 ,可以把数据改成 AA,BB 1,2,3你希望我怎么自己搭测试环境呢?宁肯让别人乱猜,你就不肯提供一下?
      

  12.   

    我的解决方法select * from (select b.disid ,a.eventid, a. typeId from Out a, Event b 
    where  a.eventId=b.id) t 
    where 
    t.disid != 3  or 
    exist (select 1 from (select b.disid , a. typeId from Out a, Event b 
    where  a.eventId=b.id) m where m.eventId=t.eventId and t.disid=3 and t.typeId!=3); 
    就是执行速度过慢 ,  几乎失去了查询的意义, 过慢!
      

  13.   

    好的 再创建 b(id int,  a int, b int )    c (id int,  a int, b int )
    select b.* from b, c where b.id=c.id  这样形成一个表(这个表就是你的t_liyihongcug)之后把这个形成的表如何融入到你的sql中 
    select * 
        -> from t_liyihongcug t 
        -> where a!=3 
        -> or exists (select 1 from t_liyihongcug where a=t.a and b!=3); 
      

  14.   

    实际情况是 b c 的字段很复杂 , 而且2个都是100多万的大表。 
    select * 
        -> from t_liyihongcug t 
        -> where a!=3 
        -> or exists (select 1 from t_liyihongcug where a=t.a and b!=3);   估计有点慢????
    我的解决方法 select * from (select b.disid ,a.eventid, a. typeId from Out a, Event b 
    where  a.eventId=b.id) t 
    where 
    t.disid != 3  or 
    exist (select 1 from (select b.disid , a. typeId from Out a, Event b 
    where  a.eventId=b.id) m where m.eventId=t.eventId and t.disid=3 and t.typeId!=3); 
    执行太慢  几乎10分钟不能数据  已经失去了查询意义
      

  15.   

    提供你的测试数据。我需要建表,需要数据。1。你提供,create table / insert 
    2。我自己编。你希望是哪一种呢?
      

  16.   

    give up db.  I will use java code