Hi,
I'm trying to write a query that selects print products for a custom based on various dropdown selects. Most of the fields are optional, and I'd like to show them close matches if there is no exact match for what they've selected.
I'm also trying to group the results by printer, so they get the single best match from each company to start with.
Here's a sample of the SQL:
This works perfectly without the first GROUP BY (the one in the outer query), it selects the correct rows with the expected calculated relevance value and puts them in order. This includes two rows from the same company, each with relevance of 1. When I add the GROUP BY back in, BOTH of these rows disappear, so no result at all come from that company... obviously not ideal. I can't work out why GROUP BY could make an entire 'group' disappear from the results?
Any suggestion would be most helpful
I'm trying to write a query that selects print products for a custom based on various dropdown selects. Most of the fields are optional, and I'd like to show them close matches if there is no exact match for what they've selected.
I'm also trying to group the results by printer, so they get the single best match from each company to start with.
Here's a sample of the SQL:
Code:
SELECT (CASE paper WHEN 'gloss' THEN 1 ELSE 0 END) as relevance, `print_products`.*, `printers`.`display_name` AS printer_display_name
FROM (`print_products`)
LEFT OUTER JOIN `printers` printers ON `printers`.`id` = `print_products`.`printer_id`
WHERE `print_products`.`product` = 'brochures'
AND `print_products`.`active` = 1
GROUP BY print_products.printer_id
HAVING relevance = (SELECT MAX((CASE paper WHEN 'gloss' THEN 1 ELSE 0 END)) FROM print_products pp WHERE pp.printer_id = print_products.printer_id and active=1 GROUP BY pp.printer_id)
ORDER BY `relevance` desc
LIMIT 10
This works perfectly without the first GROUP BY (the one in the outer query), it selects the correct rows with the expected calculated relevance value and puts them in order. This includes two rows from the same company, each with relevance of 1. When I add the GROUP BY back in, BOTH of these rows disappear, so no result at all come from that company... obviously not ideal. I can't work out why GROUP BY could make an entire 'group' disappear from the results?
Any suggestion would be most helpful