我正在尝试连接同一列中但条件不同的 2 个值。这是我的示例表。
----------------------------------
| user_id | key | value |
----------------------------------
1 firstname maria
1 lastname enuole
2 firstname chris
2 lastname magnolia
将值字段中的值与具有相同 user_id 的键名字和姓氏连接起来。抱歉,这真的很难解释。
我想要这样的结果...
--------------------------
| user_id | Name |
--------------------------
1 maria enuole
2 chris magnolia
有办法做到这一点吗?感谢您的反馈。
最佳答案
使用条件聚合
的另一种方法,但我更喜欢Group_concat
方法
select user_id, concat(F_name, ' ', L_name)
From
(
select user_id,
max(case when key = 'firstname' then value end) F_name,
max(case when key = 'lastname' then value end) as L_name
From Yourtable
Group by user_id
) A
关于mysql - 连接同一列中具有不同条件的 2 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35972073/