混合 2 个查询来显示独特的优惠券 PHP/MySql
所以我有这两个表,我正在尝试编写最有效的查询来输出每个制造商的不同优惠券:
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
此查询:
$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");
输出:
prod_id manu_id prod_type prod_coupon_img
1 2 A4 1.jpg
2 2 paper 1.jpg
3 2 boxes 2.jpg
和此查询:
$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");
输出:
manu_id prod_coupon_img
2 1.jpg
2 2.jpg
我现在想要完成的是显示独特的优惠券惠普提供优惠券,一张用于(A4 纸和纸张),一张用于盒子。 这是我输出的代码:
$DistinctCoupons .= 'Showing coupon for $row["prod_type"] products <img id="$row["prod_id"]" src="$row["prod_coupon_img"]" />';
我该如何做到这一点,如何将两个查询混合到一个查询中并输出不同的优惠券?我尝试了 2 个 while 循环,但这不起作用,而且我一直在读到无论如何都不应该在数据库调用上使用 2 个 while 循环,因为它会带来很多不必要的压力。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)