JOIN 三个 tables + GROUP_CONCAT 当有数据时 + 所有其余的第一个 table

JOIN three tables + GROUP_CONCAT when there's data + all the rest of the first table

这可能很难... 三 tables

我想检索星舰数据列表+船员(组串联)+其余没有团队的星舰记录。

此代码每行一个团队成员回馈星际飞船...

SELECT 
ss.starship_id, ss.starship_name, ss.starship_quota, ss.quota_insert_date as lastupd,
u.nome, u.cognome
FROM starship as ss
    LEFT JOIN user_props as up
    ON ss.starship_id=up.starship_id
    LEFT JOIN users as u
    ON u.id_user=up.id_user

这是示例数据:

id  name        quota   name            surname
------------------------------------------------------
23  HAS CREW    7923    Luke            Skywalker
23  HAS CREW    7923    PAdme            Amidala
------------------------------------------------------
24  UnALTRA       0     Bilbo           Baggins
24  UnALTRA       0     Frodo           Baggins
------------------------------------------------------
22  NO CREW     3552    NULL             NULL


column "lastupd" have been omitted

我想要的只是一个包含串联成员的“团队”记录,或者当没有团队在星舰上时为空。 请参阅下面的示例 table:

id  name        quota   TEAM
------------------------------------------------------
23  HAS CREW    7923    Luke Skywalker, Padme Amidala
------------------------------------------------------
24  UnALTRA       0         BilBo Baggins, Frodo Baggins
------------------------------------------------------
22  NO CREW     3552    NULL

我会为此使用相关子查询:

select s.*,
    (
        select group_concat(u.nome, ' ', u.cognome)
        from user_props up
        inner join users u on u.id_user = up.id_user
        where up.starship_id = s.starship_id
    ) team
from starship s