繁体   English   中英

计算联接中另一个表的行

[英]Count rows in a join against another table

产品表列:

  • 图片1
  • 产品类别
  • 产品数量
  • 价钱
  • 描述
  • product_id

优惠表列:

  • Offer_id
  • 产品编号
  • 提供人

我需要通过在这两个表上使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM