[英]Count rows in a join against another table
产品表列:
优惠表列:
我需要通过在这两个表上使用product_id
来获得offer_id
的计数。 我还需要按product_type
过滤产品列表。
我尝试了以下方法:
SELECT COUNT(offers_id) AS offers,
image1,
product_type,
product.amount,
product.price,
description,
product.product_id
FROM product INNER JOIN offers ON product.product_id = offers.product_id
WHERE product_type LIKE '%$product_type%'"
但是此查询仅返回产品表中的一个产品。
如果要对分组信息进行计数,则必须使用GROUP BY,例如:
SELECT COUNT(offers_id) AS offers, image1, product_type, product.amount, product.price, description, product.product_id
FROM product INNER JOIN offers ON product.product_id = offers.product_id
WHERE product_type LIKE '%$product_type%'"
GROUP BY image1, product_type, product.amount, product.price, description, product.product_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.