I'm trying to create a database where products might have more than one category assigned to them and ordered in a set way, but I'm struggling to come up with a solid way to display the results, for example:
I have a database which contains product data and it includes columns - prod_cat_1, prod_cat_2, prod_cat_1_order, prod_cat_2_order
prod_cat_1 = primary category
prod_cat_2 = secondary category
prod_cat_1_order & prod_cat_2_order = the order for each column
The above columns are set to INT(11).
So say I have this data:
prod_name prod_cat_1 prod_cat_2 prod_cat_1_order prod_cat_2_order
--------- ---------- ---------- ---------------- ----------------
product 1 cat 1 cat 2 2 2
product 2 cat 1 cat 3 3 1
product 3 cat 1 1
product 4 cat 2 cat 1 2 2
product 5 cat 3 2
product 6 cat 3 cat 2 3 1
product 7 cat 2 cat 3 1 2
product 8 cat 1 4
product 9 cat 3 cat 1 1 1
I want to order by prod_cat_1_order first and then by prod_cat_2_order.
I'm aware of:
ORDER BY prod_cat_1_order, prod_cat_2_order
But this outputs it incorrectly, as example for cat 1 below:
product 3
product 9
product 1
product 4
product 2
product 8
Also another issue I have faced is if I query cat 1 it will take all values in prod_cat_1_order into consideration, for example it would output like this:
product 3
product 9
product 1
product 4
product 2
product 8
I want to order all products assigned with cat 1 by prod_cat_1 first so all the records for cat 1 are displayed in the order of prod_cat_1_order first and then order by any order set in prod_cat_2_order afterwards.
So the output would look like this:
product 3
product 1
product 2
product 8
product 9
product 4
And then if sorting cat 2, the order would be:
product 7
product 4
product 6
product 1
And then if sorting cat 3, the order would be:
product 9
product 5
product 6
product 2
product 7
I'm not sure if what I want to do is an easy fix or something quite complicated but I'd be extremely grateful of any help or advice you could give me.
Many thanks in advance.