I have this query
SELECT
(SELECT keyword FROM url_aliAS WHERE query = CONCAT("category_id" , '=', p2c.category_id)) AS something,
p.product_id, p.model FROM product AS p
JOIN product_to_category p2c ON (p.product_id = p2c.product_id)
WHERE p.product_id NOT IN (280, 129, 131)
and the rows being returned have duplicate product_id. For example:-
something product_id model
NULL 20 18924
barcode_labels 20 18924
NULL 21 18926
barcode_labels 21 18926
NULL 22 30332
barcode_labels 22 30332
I tried DISTINCT on the product_id and I also tried in the WHERE clause “and something is not null” and both failed. Is there a solution to this?
You might want something like this (I also refactored the code a bit). However, if you have more than just
barcode_labelsas a keyword, you’ll still get an additional entry for each other combination.Another way would be to play around with the
group byclause to get what you want. Try something like this: