I have a MySQL problem which i can't figure out the solution.
I have 2 tables
Table 1-[Book] table
book id | categories | title |
1 | 1,3,5 | Book 1 |
2 | 2,4 | Book 2 |
3 | 1,4 | Book 3 |
Table 2-[Category] table
category id | category name
1 | Technology
2 | Accounting
3 | Science
4 | Math
5 | Chemistry
I need the result to show up like this
RESULT
book id | categories | title | category name
1 | 1,3,5 | Book 1 | Technology,Science,Chemistry
2 | 2,4 | Book 2 | Accounting,Math
3 | 1,4 | Book 3 | Technology,Math
I tried the below query but i'm not sure what's wrong with it.
SELECT DISTINCT t1.*,(SELECT GROUP_CONCAT(t2.categoryName) FROM `tbl_category` t2 WHERE t2.id IN (t1.categories)) catColumn FROM tbl_books t1 ORDER BY t1.id DESC
If I execute the below query, it is returning the correct values that I need:
SELECT GROUP_CONCAT(categoryName) FROM `tbl_category` t2 WHERE t2.id IN (1,3,5)
RESULT:
Technology,Science,Chemistry