I have a SQL query:
SELECT b . * , CONCAT( GROUP_CONCAT( c.name ) ) categories, CONCAT( GROUP_CONCAT( c.id ) ) cids
FROM books b
JOIN categories_of_books cb ON b.id = cb.book_id
JOIN categories c ON c.id = cb.category_id
GROUP BY b.id
And it returns to me in categories alias all categories of books and in cids all categories ids.
And when i adding WHERE clause like this:
WHERE c.id = 10
In categories I`ll have only one category and it is clear. But how can i write SQL query which will return me all categories of books in categories alias with limiting condition:
WHERE c.id = 10
[EDIT 1]
Only variant:
SELECT b . * , GROUP_CONCAT( c.name ) categories, GROUP_CONCAT(CAST(c.id AS CHAR)) cid
FROM books b
JOIN categories_of_books cb ON b.id = cb.book_id
JOIN categories c ON c.id = cb.category_id
WHERE b.id in (SELECT categories_of_books.book_id FROM categories_of_books join categories on categories_of_books.category_id = categories.id WHERE categories.id = 10)
GROUP BY b.id
Do you know other better variants?