Mysql 使用LISTAGG选择联接表上的多行

Mysql 使用LISTAGG选择联接表上的多行,mysql,sql,Mysql,Sql,假设我有以下两张表 表1-用户 ID UserName 001 abc 002 bcd 003 def 表2-价值 ID Tag Price 001 start 1 001 middle 2 001 end 3 002 start 3 002 end 4 003 start 1 003 middle 2 003 end 3 我对标记为“开始”和“结束”的值感兴趣,即预期结果是: 001 abc 1, 2 002 bcd 3, 4 003 def 1, 3 注意:我需要表1中的用户名 非常感

假设我有以下两张表 表1-用户

ID UserName
001 abc
002 bcd
003 def
表2-价值

ID Tag Price
001 start 1
001 middle 2
001 end 3
002 start 3
002 end 4
003 start 1
003 middle 2
003 end 3
我对标记为“开始”和“结束”的值感兴趣,即预期结果是:

001 abc 1, 2
002 bcd 3, 4
003 def 1, 3
注意:我需要表1中的用户名


非常感谢

你在找这样的东西吗

select u.id, u.name, listagg(price, ', ') within group (order by price) as prices
from user u left outer join
     values v
     on u.id = v.id and v.tag in ('start', 'end')
group by u.id, u.name;