Here is the DB Schema I am using (Many to Many relationship between clients and products via junction table client_product):
TABLE client
- clientid
- clientname
TABLE product
- productid
- productname
- category
TABLE client_product
- clientid*
- productid*
Category is an element of a fixed set (for this example, the set is {Food, Beverage, Books}).
I'm trying to find the most efficient way to get, for each client, his id and name, as well as (and this is the tricky part) the list of all the products he bought, sorted by category (to be displayed on a webpage via PHP, but I don't know if this is relevant). Here is a simple example of what I'd like to display:
ClientID-----Client Name-----Food------------Beverages-------------Books
-----1--------------John----------Crisps-----------Coke; Fanta----------MySQL for dummies
-----2--------------Alex-----------Pizza------------__________----------Deadpool
Now that I think about it, I'm wondering if it'd be easier to create a new table to store the categories, and add a foreign key constraint on products.category
I tried several queries but couldn't find a satisfying solution.