Hello!

Made a request:

select ta.id, ct.tag_id from table_a ta join table_tags ct on ta.id = ct.table_a_id where ct.tag_id in (1,2,3) 

enter image description here

I supplement this request with the group by cr.id line :

 select ta.id, ct.tag_id from table_a ta join table_tags ct on ta.id = ct.table_a_id where ct.tag_id in (1,2,3) group by cr.id 

Result:

How to make so that in tag_id of the second element were all three tags in one line (1, 2 and 3)? Available in varchar format

I'm not a big specialist in sql, I'm sorry if the question is banal)

    1 answer 1

    If you are using the MySQL database, use the group_concat function.

    As a result, your request will be:

     select ta.id, group_concat(ct.tag_id) as group_tag_id from table_a ta join table_tags ct on ta.id = ct.table_a_id where ct.tag_id in (1,2,3) group by ta.id 

    An example on db-fiddle.

    • Thanks, that is necessary! - Konstantin Skokov
    • @KonstantinSkokov do not forget to tick the question as a correct tick :) this will help further participants, and also ruSO will not raise the question as unanswered. - Denis