简体   繁体   English

sql 中的 Group_Concat 多列

[英]Group_Concat multple columns in sql

I have two tables.我有两张桌子。

First table is called Employee第一个表称为 Employee

  |EID| EName|  Salary |
=========================
  |1  |A     |300      |
  |2  |B     |400      | 
  |3  |O     |500      |

Second table is called Employee_Sal_Changes第二个表称为 Employee_Sal_Changes

  |EID|  Salary_Change_History| year
=======================================
  |1  |100                    |2012
  |1  |200                    |2013
  |1  |300                    |2014
  |2  |200                    |2013 
  |2  |400                    |2014 
  |3  |100                    |2011
  |3  |200                    |2012
  |3  |300                    |2013
  |3  |500                    |2014

On my front end table, I would like to show在我的前端桌子上,我想展示

 id   |Salary Change History    |
=================================
 A    | 100:2012                |
      | 200:2013                |
      | 300:2014                |
=================================
 B    | 200:2013                |
      | 400:2014                |
=================================
 O    | 100:2011                |
      | 200:2012                |
      | 300:2013                |
      | 400:2014                |
=================================

I would like to get salary_change_history and year mached to EID and show them on the front end by using loop.我想将salary_change_history 和year 匹配到EID 并使用循环在前端显示它们。

To get those, I tried to store them in an array by using group_concat but it only gets values where EID = 1为了得到这些,我尝试使用 group_concat 将它们存储在一个数组中,但它只获取 EID = 1 的值

Here is the query in the backend这是后端的查询

Select 
(select Group_Concat(Employee_Sal_Changes.Salary_Change_History),(Group_Concat(Employee_Sal_Changes.year) from Employee_Sal_Changes left join Employee on Employee_Sal_Changes.EID = Employee.EID), 
Employee.EName
From Employee_Sal_Changes

Is there a better way or just keep using group_concat?有更好的方法还是继续使用 group_concat?

You can use aggregation:您可以使用聚合:

select 
    eid, 
    group_concat(
        year, ':', salary_change_history 
        order by year
        separator '\n'
    ) salary_change_history
from employee_sal_changes
group by eid

For each employee, this generates a list of <year>:<salary> values separated by carriage returns.对于每个员工,这会生成一个由回车分隔的<year>:<salary>值列表。 Note that you don't need to bring in the employee table to generate that resultset: the other table has all necessary information.请注意,您不需要引入employee表来生成该结果集:另一个表包含所有必要的信息。

Demo on DB Fiddle : DB Fiddle 上的演示

在此处输入图像描述


If you want the employee name as well:如果您还想要员工姓名:

select 
    s.eid, 
    e.ename,
    group_concat(
        s.year, ':', s.salary_change_history 
        order by s.year
        separator '\n'
    ) salary_change_history
from employee_sal_changes s
inner join employee e on e.eid = s.eid
group by s.eid, e.ename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM