在这个例子中,我对同一个用户有不同的值(value)观。所以我必须在一行中获取所有这些值。
user_id key value
------- -------- -----------
123 name thomps
123 url thomps.com
123 email thomps@me.com
456 name hond
456 url hond.com
456 email hond@me.com
如何获取这样的合并行的列表:
user_id name url email
------- ---- ---- ------
123 thomps thomps.com thomps@me.com
456 hond hond.com hond@me.com
我尝试使用 grop_concat 并加入子查询但没有成功
最佳答案
按用户分组,然后用一个案例得到不同列的结果
select user_id,
max(case when `key` = 'name' then value end) as name,
max(case when `key` = 'url' then value end) as url,
max(case when `key` = 'email' then value end) as email
from your_table
group by user_id
关于mysql - sql - 如何在没有group_concat的情况下将不同的行合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21533215/