mysql - 将多行合并为一行 MySQL

标签 mysql rows multiple-columns

假设我在 MySQL 数据库中有两个表。

表 1:

ID    Name
1     Jim
2     Bob
3     John

表 2:

ID    key           value
1     address       "X Street"
1     city          "NY"
1     region        "NY"
1     country       "USA"
1     postal_code   ""
1     phone         "123456789"

从数据库中选择行时,有没有办法将第二个表中的行作为列连接到第一个表?

MySQL 查询的预期结果是:

ID    Name    address    city    region   country   postal_code   phone
1     Jim     X Street   NY      NY       USA       NULL          123456789
2     Bob     NULL       NULL    NULL     NULL      NULL          NULL
3     John    NULL       NULL    NULL     NULL      NULL          NULL

感谢您的帮助!

最佳答案

这种类型的数据转换称为 PIVOT。 MySQL 没有数据透视函数,但您可以使用带有 CASE 表达式的聚合函数来复制它:

select t1.id,
  t1.name,
  max(case when t2.`key` = 'address' then t2.value end) address,
  max(case when t2.`key` = 'city' then t2.value end) city,
  max(case when t2.`key` = 'region' then t2.value end) region,
  max(case when t2.`key` = 'country' then t2.value end) country,
  max(case when t2.`key` = 'postal_code' then t2.value end) postal_code,
  max(case when t2.`key` = 'phone' then t2.value end) phone
from table1 t1
left join table2 t2
  on t1.id = t2.id
group by t1.id, t1.name

SQL Fiddle with Demo

这也可以在您的 table2 上使用多个连接来编写,并且您将在每个 key 的连接上包含一个过滤器:

select t1.id,
  t1.name,
  t2a.value address,
  t2c.value city,
  t2r.value region,
  t2y.value country,
  t2pc.value postal_code,
  t2p.value phone
from table1 t1
left join table2 t2a
  on t1.id = t2a.id
  and t2a.`key` = 'address'
left join table2 t2c
  on t1.id = t2c.id
  and t2c.`key` = 'city' 
left join table2 t2r
  on t1.id = t2r.id
  and t2c.`key` = 'region' 
left join table2 t2y
  on t1.id = t2y.id
  and t2c.`key` = 'country' 
left join table2 t2pc
  on t1.id = t2pc.id
  and t2pc.`key` = 'postal_code' 
left join table2 t2p
  on t1.id = t2p.id
  and t2p.`key` = 'phone';

SQL Fiddle with Demo

如果您的 key 值数量有限,上述两个版本将非常有用。如果您有未知数量的值,那么您将需要考虑使用准备好的语句来生成动态 SQL:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(case when t2.`key` = ''',
      `key`,
      ''' then t2.value end) AS `',
      `key`, '`'
    )
  ) INTO @sql
from Table2;

SET @sql 
    = CONCAT('SELECT t1.id, t1.name, ', @sql, ' 
              from table1 t1
              left join table2 t2
                on t1.id = t2.id
              group by t1.id, t1.name;');

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

SQL Fiddle with Demo

所有版本都会给出结果:

| ID | NAME |  ADDRESS |   CITY | REGION | COUNTRY | POSTAL_CODE |     PHONE |
|----|------|----------|--------|--------|---------|-------------|-----------|
|  1 |  Jim | X Street |     NY | (null) |  (null) |      (null) | 123456789 |
|  2 |  Bob |   (null) | (null) | (null) |  (null) |      (null) |    (null) |
|  3 | John |   (null) | (null) | (null) |  (null) |      (null) |    (null) |

关于mysql - 将多行合并为一行 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21118809/

相关文章:

mysql - 如何返回不同列中的 SQL max() 值?

Postgresql select,显示固定计数行

javascript - 如何使用动态生成的行更改表(添加行跨度/列跨度)?

MySQL 仅选择分组的两行或更多行

mysql - 在MYSQL中,行数多好还是列数多好?

html - 使用 CSS 在 div 中创建 'bottom tab'?

php - 你(MySQL)可以按可选的值和不同表中的值进行排序吗?

mysql - 带有计算的sql select语句

sorting - Powershell:对具有不均匀间隔的列的文本文件进行排序

php - 从 Wamp 文件夹恢复 MySQL 数据库