MySQL将多行合并为一行并维护列名

时间:2014-12-17 16:01:49

标签: mysql

用于说明问题的表和一些虚拟数据。

成员

存储有关成员的基本信息。

------------------------------------
| member_id  | email               |
------------------------------------
| 1          | 1@a.com             |
------------------------------------
| 2          | 2@a.com             |
------------------------------------
| 3          | 3@a.com             |
------------------------------------
| 4          | 4@a.com             |
------------------------------------
| 5          | 4@a.com             |
------------------------------------

member_data

为每个成员存储一些额外的元数据

----------------------------------------
| member_id | name | surname | company |
----------------------------------------
|  1        | A    |    A    |   A     |
----------------------------------------
|  2        | B    |    B    |   B     |
----------------------------------------
|  3        | C    |    C    |   C     |
----------------------------------------
|  4        | D    |    D    |   D     |
----------------------------------------
|  5        | E    |    E    |   E     |
----------------------------------------

类别

系统内的不同类别。

------------------------------------
| cat_id | cat_name                |
------------------------------------
|   1    |  Cars                   |
------------------------------------
|   2    |  Bikes                  |
------------------------------------
|   3    |  Boats                  |
------------------------------------

许可证

会员必须拥有能够访问类别的许可。

-----------------------------------------------------------------------
| id  |  subid | catid | start_date  | end_date    | description      |
-----------------------------------------------------------------------
|  1  |   1    |   1   | 2014-01-01  | 2020-12-31  | Premium          |
-----------------------------------------------------------------------
|  2  |   1    |   2   | 2014-01-01  | 2015-12-31  | Premium          |
-----------------------------------------------------------------------
|  3  |   1    |   3   | 2014-01-01  | 2018-12-31  | Premium          |
-----------------------------------------------------------------------
|  4  |   2    |   1   | 2014-01-01  | 2016-12-31  | Premium          | 
-----------------------------------------------------------------------
|  7  |   3    |   1   | 2014-01-01  | 2014-01-02  | Premium          | 
-----------------------------------------------------------------------
|  8  |   3    |   2   | 2014-01-01  | 2014-01-02  | Premium          | 
-----------------------------------------------------------------------
|  9  |   3    |   3   | 2014-01-01  | 2020-01-31  | Premium          | 
-----------------------------------------------------------------------
|  10 |   5    |   1   | 2014-01-01  | 2014-01-02  | Premium          | 
-----------------------------------------------------------------------
|  11 |   5    |   2   | 2014-01-01  | 2014-01-02  | Premium          | 
-----------------------------------------------------------------------
|  12 |   5    |   3   | 2014-01-01  | 2014-01-02  | Premium          | 
-----------------------------------------------------------------------

关于数据

会员1拥有1,2和3类的许可。它们都是有效且有效的。 会员2仅拥有类别1的许可。它很活跃。 会员3拥有1,2和3的许可。只有第3类的许可才有效。 会员4没有许可证。 会员5拥有1,2和3类的许可证,但它们都已过期。

我正在尝试实现

我想获取每个会员许可证的记录,以及各自的member_data和类别。许可证必须存在且对于成员的类别有效,以便为该许可证返回数据。

此外,我希望返回的每个许可证都作为一行返回,其中包含以下格式所需的所有数据:

输出格式

我想输出持有有效许可证的成员,如果他们没有某个类别的许可证但是确实持有另一个类别,则返回其类别的到期日期或没有输出。即:

----------------------------------------------------------------------------------
| Company | Name | LicenceType | Cars         | Bikes          | Boats           |
----------------------------------------------------------------------------------
|   A     |A A   |  Premium    |2020-12-31    | 2015-12-31     |  2018-12-21     |
----------------------------------------------------------------------------------
|   B     |B B   |  Premium    |2016-12-31    |                |                 |
----------------------------------------------------------------------------------
|   C     |C C   |  Premium    |              |                |  2020-01-31     |
----------------------------------------------------------------------------------

我尝试过什么

SELECT
    md.company as Company,  
CONCAT(md.name,' ', md.surname) as Name,
    l.description as LicenceType,
    (CASE WHEN (c.cat_name='Cars') THEN l.end_date ELSE '' END)AS Cars,
    (CASE WHEN (c.cat_name='Bikes') THEN l.end_date ELSE '' END)AS Bikes,
    (CASE WHEN (c.cat_name='Boats') THEN l.end_date ELSE '' END)AS Boats
FROM 
    licences as l
JOIN
    categories as c ON c.cat_id=l.catid
JOIN    
    member_data as md ON md.member_id=l.subid
WHERE
    l.end_date>='2014-12-17'    
AND 
    (l.description='Premium')   
ORDER BY Company ASC    

目前输出

这是当前数据的显示方式:

----------------------------------------------------------------------------------
| Company | Name | LicenceType | Cars         | Bikes          | Boats           |
----------------------------------------------------------------------------------
|   A     |A A   |  Premium    |2020-12-31    |                |                 |
----------------------------------------------------------------------------------
|   A     |A A   |  Premium    |              | 2015-12-31     |                 |
----------------------------------------------------------------------------------
|   A     |A A   |  Premium    |              |                |  2018-12-21     |
----------------------------------------------------------------------------------
|   B     |B B   |  Premium    |2016-12-31    |                |                 |
----------------------------------------------------------------------------------
|   C     |C C   |  Premium    |              |                |  2020-01-31     |
----------------------------------------------------------------------------------

问题是,正如您可以看到A公司的记录,显示为三个不同的行。我想按照上面显示的输出格式将三行中的每一行作为一行返回。

我很欣赏有关如何实现这一目标的任何想法。谢谢。

1 个答案:

答案 0 :(得分:2)

使用聚合:

SELECT
    md.company as Company,  
    CONCAT(md.name,' ', md.surname) as Name,
    l.description as LicenceType,
    MAX(CASE WHEN (c.cat_name='Cars') THEN l.end_date ELSE '' END)AS Cars,
    MAX(CASE WHEN (c.cat_name='Bikes') THEN l.end_date ELSE '' END)AS Bikes,
    MAX(CASE WHEN (c.cat_name='Boats') THEN l.end_date ELSE '' END)AS Boats
FROM 
    licences as l
JOIN
    categories as c ON c.cat_id=l.catid
JOIN    
    member_data as md ON md.member_id=l.subid
WHERE
    l.end_date>='2014-12-17'    
AND 
    (l.description='Premium')   
GROUP BY Company, Name, l.description
ORDER BY Company ASC;