Mysql数据显示多行而不是一行

标签 mysql

enter image description here

我想知道如何根据字段值将上面的数据显示到一行中,我做了一个案例选择,但结果如下图

(SELECT lead_id,
 case lead_id
 case field_number when 1  then value  end as firstname

 case field_number when 1 then value end as firstname,
 case field_number when 2 then value end as lastname,
 case field_number when 10 then value end as email,

enter image description here

最佳答案

您正在尝试使用条件聚合来做到这一点(这意味着您缺少一个 group by 子句和一个最大/最小函数):

SELECT lead_id,
       MAX(CASE WHEN field_number = 1 then value end) as firstname,
       MAX(CASE WHEN field_number = 2 then value end) as lastname,
       MAX(CASE WHEN field_number = 10 then value end) as email
FROM YourTable
GROUP BY lead_id

关于Mysql数据显示多行而不是一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36263121/

相关文章:

mysql - 这是 WooCommerce/WordPress 数据库中的订单和购买用户之间的关联

mysql - 查询中的棘手条件

mysql - 给定查询中的错误是什么

php - 更新多个查询时,更新 php mysqli 中的行计数不起作用

php - 在mysql上用foreach显示数据数组

php - 尝试将托管 Web 应用程序连接到本地数据库?

mysql - 在 MySQL 中,是否可以将变量的值命名为列?

mysql - 在 sequelize 中使用 where 子句 with 或 condition

MySQL select into outfile "file already exists"和 "file does not exist"对偶(?)案例

mysql - 如何保持wordpress、AWS lightsail的两个MySQL数据库同步?