gpt4 book ai didi

MySQL avg 和 left join

转载 作者:行者123 更新时间:2023-11-29 11:38:10 25 4
gpt4 key购买 nike

有没有办法结合这两个查询?添加另一个左连接不起作用。

 select distinct 
p.products_id,
p.products_image,
pd.products_name,
m.manufacturers_name,
p.manufacturers_id,
p.products_price,
p.products_tax_class_id,
IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price) as final_price
from products_description pd,
products p
left join manufacturers m on p.manufacturers_id = m.manufacturers_id
left join specials s on p.products_id = s.products_id, products_to_categories p2c
where
p.products_status = '1'
and p.products_id = p2c.products_id
and pd.products_id = p2c.products_id
and pd.language_id = '1'
and p2c.categories_id = '17'
order by p.sort_order
//-------------------
select avg(reviews_rating) as average_rating from reviews where
products_id = '31' and reviews_status = '1'

第一个是获取产品详细信息,第二个是从评论表中获取平均评分。

最佳答案

你可以尝试一下-

SELECT p.products_id, p.products_image, pd.products_name, m.manufacturers_name, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) AS specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) AS final_price, AVG(rvw.reviews_rating) AS average_ratings  
FROM products p
JOIN products_to_categories p2c ON p.products_id = p2c.products_id
JOIN products_description pd ON pd.products_id = p2c.products_id
LEFT JOIN manufacturers m ON p.manufacturers_id = m.manufacturers_id
LEFT JOIN specials s ON p.products_id = s.products_id
LEFT JOIN reviews rvw ON rvw.products_id=p.products_id
WHERE p.products_status = '1' AND pd.language_id = '1' AND p2c.categories_id = '17'
GROUP BY p.products_id
ORDER BY p.sort_order;

注意:始终首先提及所有内部/逗号连接表,然后是左连接。

关于MySQL avg 和 left join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36280685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com