2

来自如下所示的数据(在 MySQL 数据库中):

mysql> select * from test;
+------------+--------+
| start      | name   |
+------------+--------+
| 2013-04-01 | Donald |
| 2013-04-02 | Daisy  |
| 2013-04-03 | Mickey |
| 2013-04-03 | Minnie |
| 2013-04-01 | Pluto  |
| 2013-04-02 | Goofy  |
+------------+--------+
6 rows in set (0.00 sec)

我想编写一个查询来生成如下所示的输出:

2013-04-01  2013-04-02  2013-04003
----------  ----------  ----------
Donald      Daisy       Mickey
Pluto       Goofy       Minnie

有人可以建议 SQL 查询来产生这个输出吗?

如果有帮助,这是我用来创建示例数据的代码。

create table test (
    start date,
    name varchar (20));

insert into test values ('2013-04-01','Donald');
insert into test values ('2013-04-02','Daisy');
insert into test values ('2013-04-03','Mickey');
insert into test values ('2013-04-03','Minnie');
insert into test values ('2013-04-01','Pluto');
insert into test values ('2013-04-02','Goofy');`
4

2 回答 2

1

从给定的数据结构中,您无法获得所需的输出。因为记录之间没有任何关系(例如 Id 等)

如果我们这样使用GROUP_CONCAT()

Select 
      GROUP_CONCAT(Case When Start = '2013-04-01' 
                        Then name ELSE NULL End) As '2013-04-01',
      GROUP_CONCAT(Case When Start = '2013-04-02' 
                        Then name End) As '2013-04-02',
      GROUP_CONCAT(Case When Start = '2013-04-03' 
                        Then name End) As '2013-04-03'
From Test;

然后你会得到这样的输出:

|   2013-04-01 |  2013-04-02 |    2013-04-03 |
----------------------------------------------
| Donald,Pluto | Daisy,Goofy | Mickey,Minnie |

这是一个动态查询:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'GROUP_CONCAT(CASE WHEN Start = ''',
      `Start`,
      ''' THEN `name` ELSE NULL END) AS `',
      `Start`, '`'
    )
  ) INTO @sql
FROM Test;

SET @sql = CONCAT('SELECT ', @sql,'
                     FROM Test
                  ');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

看到这个 SQLFiddle

如果有一个字段,ID你可以这样做:

Select ID,
      GROUP_CONCAT(Case When Start = '2013-04-01' 
                        Then name ELSE NULL End) As '2013-04-01',
      GROUP_CONCAT(Case When Start = '2013-04-02' 
                        Then name End) As '2013-04-02',
      GROUP_CONCAT(Case When Start = '2013-04-03' 
                        Then name End) As '2013-04-03'
From Test2
GROUP BY ID;

或者像这样的动态查询:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'GROUP_CONCAT(CASE WHEN Start = ''',
      `Start`,
      ''' THEN `name` ELSE NULL END) AS `',
      `Start`, '`'
    )
  ) INTO @sql
FROM Test2;

SET @sql = CONCAT('SELECT ID, ', @sql,'
                     FROM Test2
                  GROUP BY ID
                  ');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

输出:

| ID | 2013-04-01 | 2013-04-02 | 2013-04-03 |
---------------------------------------------
|  1 |     Donald |      Daisy |     Mickey |
|  2 |      Pluto |      Goofy |     Minnie |

看到这个 SQLFiddle

于 2013-07-18T04:03:26.357 回答
1

将不得不有一个数据透视表或一系列案例陈述。

Select 
      Case When Start = '2013-04-01' Then name End As '2013-04-01',
      Case When Start = '2013-04-02' Then name End As '2013-04-02',
      Case When Start = '2013-04-03' Then name End As '2013-04-03'
From Test;

如果表更复杂,动态生成它会有点困难。通过简单地导出到 Excel 并使用数据透视表功能,使用更大的数据更容易做到这一点。

于 2013-07-18T03:34:06.577 回答