Help me, there are two tables,

orgtypes - Contains types of organizations

 id |name | -------------------------- 1 |Организация 1| 1 |Организация 1| 1 |Организация 1| 1 |Организация 1| 

orgs - contains selected types of organizations

 id |user_id | types -------------------------- 1 |1 |1 1 |1 |3 1 |2 |2 1 |2 |4 

Need to get a table

 id | user | ogrs -------------------------- 1 | 1 | Организация 1,Организация 3 1 | 2 | Организация,Организация 4 

So here. I can not understand how JOIN does not fit because they do not have a common column, (SELECT orgs.user_id, GROUP_CONCAT(orgs.types) FROM orgs WHERE orgs.types IN( SELECT orgtypes.name,orgtypes.name FROM orgtypes)) ?? unclear

    1 answer 1

    You can do the following:

     SELECT orgs.id AS id, orgs.user_id AS user_id, CONCAT_GROUP(orgtypes.name) AS ors FROM orgs LEFT JOIN orgtypes ON ors.types = orgtypes.id GROUP BY orgs.user_id 

    It’s not necessary that the keys are called the same; you simply cannot use the USING keyword, but you can replace it with the ON keyword. You can link tables through the primary key of the orgtypes.id type orgtypes.id and the foreign key of the organizations of the link table ors.types .

    Since you need to list the organizations separated by commas, it is convenient to group the results by GROUP BY orgs.user_id and use the CONCAT_GROUP() function to display the list of organizations within each of the received groups.

    • Thank you kind people, like this, edited and how it works SELECT orgs.id AS id, orgs.user_id AS uid, GROUP_CONCAT (orgtypes.name) AS orgs FROM orgs LEFT JOIN orgtypes ON orgs.val = orgtypes.id GROUP BY orgs. user_id - Cone Enoc