有没有办法将这两个查询结合起来?添加另一个左连接不起作用。
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'
第一个是获取产品详细信息,第二个是获得评论表的平均评分。
答案 0 :(得分: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;
注意:始终首先提及所有内部/逗号连接表,然后左键加入。
答案 1 :(得分:0)
如何将第二个查询作为select item
放在第一个查询中?
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,
(select avg(reviews_rating) as average_rating from reviews where products_id = '31' and reviews_status = '1') as average_rating
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
答案 2 :(得分:0)
M在查询中没什么好处,但会尝试一下。 首先,我想知道表格与评论之间是否存在关系。和' PRODUCT_DESCRIPTION'。如果是,那么您的组合查询可能是:
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 , avg(rv.reviews_rating) as average_rating from products_description pd, reviews rv , manufacturers m, products p, specials s, products_to_categories p2c where pd.products_id = rv.products_id and p.manufacturers_id = m.manufacturers_id and p.products_id = s.products_id and 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' and rv.products_id = '31' and rv.reviews_status = '1' order by p.sort_order;