表如下
id type
1  0
2  1
3  0
4  4
5  0
6  4我想要列出所有type=0的项目,如果type>0那么列出消重后的,出来的结果如下id type
1  0
2  1
3  0
4  4
5  0
删掉了id=6,type=4,,因为type=4的有id=4.消去重复的求哪位大哥可以写一条sql啊,不用union all可以嘛?

解决方案 »

  1.   

    补充一下,就是用
    select * from table group by type.
    不过type=0的就不要消去了这可以设置条件嘛?
      

  2.   

    为什么要排斥 union all 呢?假设你的 id 均大于 0,写出如下这么诡异的语句,Mysql 5.1 测试通过:select * 
    from 你的表
    group by type, if(type = 0, id, -1)
    order by id
      

  3.   

    select a.id,a.type from test20 a left join
    (select max(id) id,type,count(type) cnt from test20 where type>0 group by type having cnt>1) b
    on a.id=b.id where b.id is null