mysql 一个结果行一张表中的多行

标签 mysql sql

我有一个如下所示的表格:


    + ------- + -----------+ ------------ + --------- + ---------- +
    | Info_Id | category   | EAN          | Info_Type | Info_Value |
    + ------- + -----------+ ------------ + --------- + ---------- +
    | 1       | 1          | 0123456789   | brand     | brand1     |
    | 2       | 1          | 0123456789   | type      | type1      |
    | 3       | 1          | 0123456789   | price     | 0.00       |
    | 4       | 2          | 9876543210   | brand     | brand6     |
    | 5       | 2          | 9876543210   | type      | type3      |
    | 6       | 2          | 9876543210   | price     | 15.00      |
    | 7       | 2          | 6548214656   | brand     | brand34    |
    | 8       | 2          | 6548214656   | type      | type1      |
    | 9       | 2          | 6548214656   | price     | 99.00      |
    | 10      | 3          | 245511411241 | brand     | brand324   |
    | 11      | 3          | 245511411241 | type      | type1      |
    | 12      | 3          | 245511411241 | price     | 98.00      |
    + ------- + -----------+ ------------ + --------- + ---------- +

现在,我正在寻找一个创建以下输出的查询:

    + ------------ + ---------- + --------- + ---------- + ----------+
    | EAN          | category   | brand     | type       | price     |
    + ------------ + ---------- + --------- + ---------- + ----------+
    | 0123456789   | 1          | brand1    | type1      | 0.00      |
    | 9876543210   | 2          | brand6    | type3      | 15.00     |
    | 6548214656   | 2          | brand34   | type1      | 99.00     |
    | etc.         | etc.       | etc.      | etc.       | etc.      |
    + ------------ + ---------- + --------- + ---------- + --------- +

我有以下内容,但这似乎不起作用:


    SELECT ean,
           GROUP_CONCAT(Info_Type)    AS Info_Type,
           GROUP_CONCAT(Info_Value) AS Info_Value
    FROM tablename WHERE category=2
    GROUP BY EAN

但是,这为我提供了类似的东西:

    + ------------ + ---------------- + ------------------- + 
    | EAN          | Info_Type        | Info_Value          | 
    + ------------ + ---------------- + ------------------- +
    | 0123456789   | brand,type,price | brand1,type1,0.00   |
    | 9876543210   | brand,type,price | brand6,type3,15.00  |
    | 6548214656   | brand,type,price | brand34,type1,99.00 |
    | etc.         | etc.             | etc.                |
    + ------------ + ---------------- + ------------------- +

我该如何正确地做到这一点?

最佳答案

我认为你只想要条件聚合:

select ean, category,
       max(case when info_type = 'brand' then info_value end) as brand,
       max(case when info_type = 'type' then info_value end) as type,
       max(case when info_type = 'price' then info_value end) as price
from t
where category = 2
group by ean, category;

关于mysql 一个结果行一张表中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43016923/

相关文章:

mysql - Hibernate 条件示例查询获取多条记录

php - 找不到存储过程中的错误

php - 我需要一个函数将数据从一个表附加到另一个表

mysql - 打开的数据库连接数

MySQL 动态数据透视表

mysql - 按日期和字段排序

c# - 更改 css 样式表并将所选样式表保存到数据库

mysql - select 中的 select 语句

php - INSERT 上的 SQL 注入(inject)

php - 将特定格式的日期插入 MySQL 数据库