连接两个具有相同 FK 的字符串

Concat two strings with same FK

我在 mysql

中有以下表格
table payment_enum(
id int PK,
name text,
category_id int FK to category
)

table category(
id int PK,
category_name text
)

我想要的是:在 payment_enum 中可以有两个名称不同但名称相同的列 category_id 例如:

select * from payment_enum 
pk   name  category_id
1    'first'  2
2    'second' 2

PK = 2 的类别名称为 'laptops' 是否可以使用会产生以下结果的查询

category_name category_PK enum_names
laptops        2            first,second 

伪查询

select c.name,c.pk, e.name as (concat (e.name,concat(',',select e2.name where e2.name like e.name from payment_enum as e2)))
 from category as c and payment_enum as e

您需要 group_concat 而不是 concat

select c.name,group_concat(e.name) as enum_names
from category as c
left join payment_enum as e on c.id = e.category_id
group by c.name

试试这个:

select c.name as category_name ,c.pk as category_PK , GROUP_CONCAT(p.name) as enum_names from
(select * from category) as c
left JOIN
(select * from payment_enum) as p
on c.id = p.category_id GROUP BY  c.name,c.pk