在mysql中以某种方式对数据进行分组

时间:2014-11-19 11:00:54

标签: mysql

我有一个查询从一个名为supervisor_approvals的表中获取数据,我想将某个部门的行组合在一起,并为每个labref保留其他部门未分组

    say supervisor approval structure looks like this
    -------------------------------------------------
   labref   department
    ab        1
    ab        1
    ab        0
    ab        0
    ab        0
    xy        1
    xy        0
    xy        0

查询后(结果应该是)

labref     department

  ab          0
  ab          1
  ab          1
  xy          0
  xy          1

相反,它不显示其他labref数据,只显示a http://sqlfiddle.com/#!2/6fb86/9所示的第一个数据!

我的sql

SELECT DISTINCT *
                FROM supervisor_approvals 
                WHERE department = 0
                AND assign_status='0' 
                GROUP BY department 
                UNION ALL SELECT * 
                FROM supervisor_approvals 
                WHERE department = 1
                AND assign_status='0' 
                ORDER BY department DESC

1 个答案:

答案 0 :(得分:0)

尝试:

SELECT * FROM supervisor_approvals group by labref,department