【问题标题】:How to make a "distinct" join with MySQL如何使用 MySQL 进行“不同”连接
【发布时间】:2011-03-06 13:05:06
【问题描述】:

我有两个想要加入的 MySQL 表(产品和价格历史记录):

Product表:

Id = int
Name = varchar
Manufacturer = varchar
UPC = varchar
Date_added = datetime

Price_h表:

Id = int
Product_id = int
Price = int
Date = datetime

我可以执行一个简单的 LEFT JOIN:

SELECT Product.UPC, Product.Name, Price_h.Price, Price_h.Date
FROM Product
LEFT JOIN Price_h
ON Product.Id = Price_h.Product_id;

但正如预期的那样,如果我在价格历史表中有多个产品条目,我会为每个历史价格获得一个结果。

如何构造一个只返回每个产品的一个实例的连接结构,其中只连接价格历史表中的最新条目?

【问题讨论】:

    标签: mysql join distinct left-join subquery


    【解决方案1】:

    试试这个:

    SELECT Product.UPC, Product.Name, Price_h.Price, MAX(Price_h.Date)
     FROM Product
     INNER JOIN Price_h
       ON Product.Id = Price_h.Product_id
    GROUP BY Product.UPC, Product.Name, Price_h.Price
    

    【讨论】:

    • 这行得通,但它不会为他提供与最大(日期)相对应的正确价格历史条目...
    • 此查询返回每个 (Product.UPC+Product.Name+Price_h.Price) 的 Max(date)。这不是产品的最新价格。
    • Ariel 和 a1ex07 是正确的。这个查询确实返回了正确的日期,但不是正确的价格。
    【解决方案2】:
    SELECT Product.UPC, Product.Name, Price_h.Price, Price_h.Date
    FROM Product
    LEFT JOIN Price_h
    ON (Product.Id = Price_h.Product_id AND Price_h.Date = 
      (SELECT MAX(Date) FROM Price_h ph1 WHERE ph1.Product_id = Product.Id));
    

    【讨论】:

      【解决方案3】:

      用途:

         SELECT p.upc,
                p.name,
                ph.price,
                ph.date
           FROM PRODUCT p
      LEFT JOIN PRICE_H ph ON ph.product_id = p.id
           JOIN (SELECT a.product_id, 
                        MAX(a.date) AS max_date
                   FROM PRICE_H a
               GROUP BY a.product_id) x ON x.product_id = ph.product_id
                                       AND x.max_date = ph.date
      

      【讨论】:

      • 我明白你为什么说它应该更快,但我还没有找到它所在的情况。我认为这可能与我在价格历史表中的大量记录(3.5M)有关。如果我在 Price_h.date 列中添加索引可能会有所帮助?
      • 无论返回多少结果,此查询似乎花费的时间大致相同,而 a1ex07 的查询随着结果的增加而花费更长的时间,正如预期的那样。对于小型结果集的速度有多慢以及 a1ex07 的解决方案对于相同的小型结果集的速度有多快,我感到有点惊讶。因为在“现实世界”中我将使用相当大的结果集,所以我选择了这个作为答案。
      • 你拯救了我的一天 :),谢谢
      【解决方案4】:
      SELECT n.product_id, 
             n.product_name,
             n.product_articul,
             n.product_price,
             n.product_discount,
             n.product_description, 
             n.product_care,
             (SELECT photo_name FROM siamm_product_photos WHERE product_id = n.product_id LIMIT 1) AS photo_name
      FROM siamm_product as n;
      

      【讨论】:

        【解决方案5】:

        为什么不让它简单快速:

        SELECT 
         Product.UPC, Product.Name, Price_h.Price, Price_h.Date
        FROM 
         Product
        LEFT JOIN 
         Price_h
         ON Product.Id = Price_h.Product_id;
        ORDER BY
         Price_h.Date DESC
        LIMIT 1
        

        【讨论】:

          猜你喜欢
          • 2015-08-01
          • 2015-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-01
          相关资源
          最近更新 更多