So I have these 2 tables and I’m trying to write the most efficient query to output the distinct coupons for each manufacturer:
Manu Table
manu_id manu_name
1 Fujitsu
2 HP
3 Brother
Products Table
prod_id manu_id prod_type prod_coupon_img
1 2 A4 1.jpg
2 2 paper 1.jpg
3 2 boxes 2.jpg
4 3 ink 3.jpg
This query:
$sql = mysql_query("SELECT m.manu_id, p.prod_id, p.prod_type, p.prod_coupon_img
FROM manu m INNER JOIN products p
WHERE m.manu_id=2 AND m.manu_id=p.manu_id");
Outputs:
prod_id manu_id prod_type prod_coupon_img
1 2 A4 1.jpg
2 2 paper 1.jpg
3 2 boxes 2.jpg
and this query:
$sqlDistinctCoupons = mysql_query("SELECT DISTINCT m.manu_id, p.prod_coupon_img
FROM manu m INNER JOIN products p
WHERE m.manu_id=2 AND m.manu_id=p.manu_id");
Outputs:
manu_id prod_coupon_img
2 1.jpg
2 2.jpg
What I’m trying to accomplish now is to show the unique coupons HP is offering, so one for (A4 & paper), and one for boxes.
Here is my code to output that:
$DistinctCoupons .= 'Showing coupon for $row["prod_type"] products <img id="$row["prod_id"]" src="$row["prod_coupon_img"]" />';
How can I do this, how can I mix both queries to just one and output the distinct coupons? I tried 2 while loops but that didn’t work, and I’ve been reading that you shouldn’t use 2 while loops on db calls anyways, as it puts a lot of unneeded strain. Any ideas?
1 Answer