6

我想对字段进行分组,但想显示名为的列的所有行remain

这是没有分组的当前输出:

在此处输入图像描述

并与组输出:

在此处输入图像描述

当我分组时我想要这个:

在此处输入图像描述

这是mysql查询:

SELECT staff.name,
  staff.designation_id_designation,
  staff.status_id_status,
  Concat_Ws('=', leave_remain.leave_type_id_leave_type, leave_remain.days) AS
  remain
FROM staff INNER JOIN
  leave_remain ON staff.id_staff = leave_remain.staff_id_staff
GROUP BY staff.name    
4

3 回答 3

9

利用GROUP_CONCAT

SELECT  staff.name,
        staff.designation_id_designation,
        staff.status_id_status,
        GROUP_CONCAT(Concat_Ws('=', leave_remain.leave_type_id_leave_type, leave_remain.days)) AS remain
FROM    staff 
        INNER JOIN leave_remain 
            ON staff.id_staff = leave_remain.staff_id_staff
GROUP   BY staff.name, staff.designation_id_designation, staff.status_id_status  

其他链接

于 2013-02-11T05:55:43.100 回答
1

尝试这个:

SELECT staff.name,
  staff.designation_id_designation,
  staff.status_id_status,
  GROUP_CONCAT(Concat_Ws('=', leave_remain.leave_type_id_leave_type, leave_remain.days) SEPARATOR ' ') AS remain
FROM staff INNER JOIN
  leave_remain ON staff.id_staff = leave_remain.staff_id_staff
GROUP BY staff.designation_id_designation,
         staff.name,
         staff.status_id_status;
于 2013-02-11T05:54:19.810 回答
1

为此,我们需要使用 group_contact 以“”分隔。

SELECT staff.name,
  staff.designation_id_designation,
  staff.status_id_status,
  GROUP_CONCAT(Concat_Ws('=', leave_remain.leave_type_id_leave_type, leave_remain.days) SEPARATOR ' ') AS remain
FROM staff 
INNER JOIN
  leave_remain 
ON 
staff.id_staff = leave_remain.staff_id_staff
GROUP BY staff.designation_id_designation,
         staff.name,
         staff.status_id_status;
于 2013-02-11T06:00:30.480 回答