你的意思是这样的:
select
a.project_id,
b.list_id,
c.item_id
from
projects a
join lists b
on a.project_id=b.project_id
join items c
on b.list_id=c.list_id
输出将类似于:
project_id | list_id | item_id
1 | 5 | 45
1 | 5 | 46
1 | 8 | 12
还是您想在一行中返回它的所有部分?
如果你想要一行,你可以按照以下方式做一些事情:
select
a.project_id,
group_concat(b.list_id) as listIDs,
group_concat(c.item_id) as itemIDs
from
projects a
join lists b
on a.project_id=b.project_id
join items c
on b.list_id=c.list_id
但是在 PHP 中处理所有分组的东西会变得更加混乱。
输出将类似于:
project_id | list_id | item_id
1 | 5,8 | 45, 46, 12
您还可以混合搭配两者,以获得两全其美的效果:
select
a.project_id,
b.list_id as listIDs,
group_concat(c.item_id) as itemIDs
from
projects a
join lists b
on a.project_id=b.project_id
join items c
on b.list_id=c.list_id
输出将类似于:
project_id | list_id | item_id
1 | 5 | 45, 46
1 | 8 | 12