我有一个需要查询的应用程序,有两列。这是我的两列和各自的行:

Name of table1: Machines(has a row called Machinesnames and a id_group as FK)


Name of table2: Groups (has a row called groupsnames and id_groups as PK)


问题是,您在下面看到的查询中,我得到以下结果

**GroupsNames**  |   **MachinesNames**
       1                machine1

       1         |      machine2

       1         |      machine3

       2         |      machine4


我已经做到了,但是我认为错了,您可以更正我的查询吗?:

SELECT groups.name,Machines. Machinesnames,Groups.groupsnames FROM Machines INNER JOIN Groups ON Machines.id_group = Groups.id_group


这是我想看到的结果

     **GroupsNames**  |   **MachinesNames**
           1            machine1,machine2,machine3

           2          |   machine4

最佳答案

您正在寻找group_concat

select g.name,
    group_concat(m.Machinesnames)
from Machines m
inner join Groups g on m.id_group = g.id_group
group by g.name;

09-19 04:37