My first post here.
I am trying to sort values displayed from my products table with values inside my products_categories table.
my products table contains the following fields:
id | psku | pname | pdescription
My products_categories table has the following fields:
id | psku | subcategory | orderid
The site I am building is a small eshop, which contains several subcategories. The setup at this time allows me to have 1 product added to multiple categories.
For example, product sku# 6001 would be listed in 2 subcategories, cosmetics & eyelashes. I would have then 2 entries in my products_subcategories table like so:
id | psku | subcategory | orderid
1 | 6001 | cosmetics | 1
2 | 6004 | eyelashes | 1
3 | 6003 | cosmetics | 2
4 | 6011 | cosmetics | 3
5 | 6020 | eyelashes | 2
6 | 6045 | cosmetics | 4
I am currently using the following query to display data in each subcategory:
SELECT * FROM products LEFT JOIN products_subcategories
ON products.psku = products_subcategories.psku
WHERE products.psku IN ('6001','6003','6011','6020','6045')
GROUP BY e2o_products_subcategories.psku
ORDER BY e2o_products_subcategories.orderid ASC
This should display my result as so:
Cosmetics:
PSKU 6001
PSKU 6003
PSKU 6011
PSKU 6045
eyelashes:
PSKU 6004
PSKU 6020
It doesn’t seem to work. Tried some other combinations, with worst result. The products show, but will not sort by orderid
Would someone have a possible solution to this problem. I can provide sample data if needed.
Thank you.
I have had a look at the sampledata.php file that you have created. I am struggling to make sense of what you are trying to do. Why are you running two queries? Surely you could achieve the same result with a single query.
Your first query will return a list of pskus that you are then passing into the second query –
The second query is then –
Given that you are only retrieving the fields from the products table you can just do –