关系中的MYSQL查询计算

时间:2017-01-06 06:05:19

标签: php mysql sql database

MySQL架构

product_categories: 
id, name

products
id,category_id,name,description

product_offers
id,product_id,name,email

product_offer_negotiations
id,product_offer_id,offer(float),by_user(ENUM-Seller/buyer),status

这是我试过的查询...

select products.*, 
product_categories.name as category_name, 
COUNT(product_offers.id) AS total_offers, 
ROUND( AVG(product_offer_negotiations.offer), 2) AS avg_offer 
from products 
inner join product_categories on product_categories.id = products.category_id 
inner join product_offers on product_offers.product_id = products.id 
inner join product_offer_negotiations on product_offer_negotiations.product_offer_id = product_offers.id 
and product_offer_negotiations.by_user = ? 
group by products.id;

我想从产品表,total_offers和avg_offer产品中获取所有字段(每个offer_id只有协商表中的最高值 - 没有重复)

此查询仅返回产品表的第一行。谢谢你的帮助。

0 个答案:

没有答案