Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql - group, but show all row of a column

I want to group fields but want to show all row of a column called remain:

here is current output without grouping:

enter image description here

And output with group:

enter image description here

And I want This when I group:

enter image description here

Here is mysql query:

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    
like image 844
user007 Avatar asked Feb 11 '13 05:02

user007


3 Answers

use 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  
  • SQLFiddle Demo

other Link(s)

  • MySQL GROUP_CONCAT
like image 194
John Woo Avatar answered Sep 30 '22 18:09

John Woo


Try this:

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;
like image 43
Hamlet Hakobyan Avatar answered Sep 30 '22 19:09

Hamlet Hakobyan


TRy this for this we need to use group_contact seperate by ' '.

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;
like image 22
user2001117 Avatar answered Sep 30 '22 18:09

user2001117