O
O
Optimus2018-01-28 13:36:54
MySQL
Optimus, 2018-01-28 13:36:54

How to choose articles with tags?

table1 - table with articles
table2 - article-tag relationship table (2 columns id_art - article id, id_tag ​​- tag id) table3 -
table with tags tags and for each tag also a name from table3? It didn’t even work out for me to pull up the id, I tried this:
SELECT * FROM `table1` LIMIT 10

SELECT * FROM `table1` LIMIT 6 LEFT JOIN `table2` WHERE `table1`.`id` = `table2`.`art_id`

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman MySQL, 2018-01-28
Pyan @marrk2

like this

SELECT
 arts.id_art,
 COALESCE(GROUP_CONCAT(tags.name SEPARATOR ','), '')
FROM table1 arts
LEFT JOIN table2 _t ON _t.id_art = arts.id_art
LEFT JOIN table3 tags ON tags.id_tag = _t.id_tag
GROUP BY arts.id_art

get for each article a list of its tag names separated by commas or an empty string if there are none

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question