【问题标题】:MySQL avg and left joinMySQL 平均和左连接
【发布时间】:2016-03-29 18:16:08
【问题描述】:

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

 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'

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

【问题讨论】:

  • 每个查询的结果是什么样的?合并后的结果会是什么样子?

标签: mysql


【解决方案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;

注意:始终首先提及所有内部/逗号联接表,然后再离开联接。

【讨论】:

    【解决方案2】:

    将第二个查询作为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
    

    【讨论】:

      【解决方案3】:

      M 不擅长查询,但会试一试。 首先,我想知道表'REVIEWS'和'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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-29
        • 2012-11-13
        • 1970-01-01
        相关资源
        最近更新 更多