I have the following problem: I have three tables, a users table, a categories table and a user_category table which connects the two first tables with foregn keys.
users table:
id username
1 user1
... ...
categories table:
id category
1 test1
2 test2
3 test3
... ...
user_category table
id user_id category_id
1 1 1
2 1 2
3 1 3
Now I want to select all users with their categories, but I dont want to have multiple rows of the same user - instead, I want to merge all categories of a user into one category field.
SELECT users.*, categories.category FROM user_category INNER JOIN users ON users.id = user_category.user_id LEFT JOIN categories ON categories.id = user_category.category_id
The output:
id username category
1 user1 test1
1 user1 test2
1 user1 test3
But I want the following:
id username category
1 user1 test1, test2, test3
Is there a possibility to do this?