2

我一直在寻找2天的答案,但仍然一无所获。请帮我。

我有一个包含产品、产品价格和价格注册日期的数据库:

product_id | price | date
-------------------------
    1      | 8.95  | 2012-12-01
    2      | 3.40  | 2012-12-01
    1      | 9.05  | 2012-12-19
    3      | 2.34  | 2012-12-24
    3      | 2.15  | 2012-12-01
    1      | 8.80  | 2012-12-19
    1      | 8.99  | 2012-12-02
    2      | 3.45  | 2012-12-02

请注意,同一天的产品可能具有不同的价格值(第 3 行和第 6 行)。这是因为单一产品有许多供应商。数据库上也有一个供应商列,但我发现它与解决方案无关。如果我错了,您可以将其添加到解决方案中。

基本上我想要的是编写一个返回两个组合数据集的查询,如下所示:

  • 第一套是按上个月插入的产品的最低价格制作的。由于今天是 1 月 15 日,查询应读取第 3、4 和 6 行,应用最低价格,并仅返回第 4 和第 6 行,均包含该产品上个月的最低价格。
  • 第二套是最后插入的产品,上个月没有注册价格。即,对于第一组中未显示的产品,查询应搜索最后插入的产品。

我希望这很清楚。如果不是,请再问我。

该数据库的查询结果应该是:

product_id | price | date
-------------------------
    1      | 8.80  | 2012-12-19 <-Min price for product 1 on last month
    3      | 2.34  | 2012-12-24 <-Min price for product 3 on last month
    2      | 3.45  | 2012-12-02 <-No reg for product 2 on last month, show last reg.

我已经尝试了所有方法:UNION,(DATE_SUB(CURDATE(),INTERVAL 1 MONTH),MIN(价格),MAX(日期)等。没有任何效果。我现在不知道在哪里搜索,请帮助我。

4

4 回答 4

1
(SELECT product_id, MIN(price), date
FROM products
WHERE date + INTERVAL 1 MONTH > NOW()
GROUP BY product_id)
UNION
(SELECT product_id, price, MAX(date)
FROM products
WHERE product_id NOT IN (SELECT product_id
    FROM products
    WHERE date + INTERVAL 1 MONTH > NOW()
    GROUP BY product_id)
GROUP BY product_id)

这应该可行,但我不确定这是最优化的方法。

于 2013-01-15T14:39:55.977 回答
0

将其分解为几个子查询:

  • 上个月价格的产品,最低价格
    • 以该价格加入日期

联盟

  • 上个月无价商品,最大日期
    • 在那个日期加入价格

SQL小提琴

这里

询问

SELECT MINPRICE.product_id, P.date, MINPRICE.price
    FROM
    (
        -- Min price in last 31 days
        SELECT product_id, MIN(price) AS price
            FROM Prices
            WHERE DATEDIFF(CURDATE(), date) < 31
            GROUP BY product_id
    ) MINPRICE
    -- Join in to get the date that the price occured on
    INNER JOIN Prices P ON
        P.product_id = MINPRICE.product_id
        AND
        P.price = MINPRICE.price
UNION
SELECT MAXDATE.product_id, MAXDATE.date, P.price
    FROM
    (
        -- Product with no price in last 31 days - get most recent date
        SELECT product_id, MAX(date) AS date
            FROM Prices
            WHERE product_id NOT IN
            (
                SELECT product_id
                    FROM Prices
                    WHERE DATEDIFF(CURDATE(), date) < 31
            )
    ) MAXDATE
    -- join in price on that date
    INNER JOIN Prices P ON
        P.product_id = MAXDATE.product_id
        AND
        P.date = MAXDATE.date
于 2013-01-15T14:23:25.190 回答
0

像这样的东西可以解决问题:

SELECT * FROM ( 
    SELECT DISTINCT b.product_id, IF (c.min IS NULL,(SELECT ROUND(e.price,2) FROM products AS e WHERE e.product_id = b.product_id ORDER BY e.date DESC LIMIT 1 ),c.min) AS min, IF (c.date IS NULL,(SELECT f.date FROM products AS f WHERE f.product_id = b.product_id ORDER BY f.date DESC LIMIT 1 ),c.date) AS date, IF(c.min IS NULL,'<-No reg for product 2 on last month, show last reg.','<-Min price for product 1 on last month') as text FROM products AS b
    LEFT JOIN
        (SELECT a.product_id, round(MIN(a.price),2) AS min, a.date FROM products AS a WHERE a.date BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND CURDATE() GROUP BY a.product_id) AS c 
        ON (b.product_id = c.product_id)
    ) AS d
    ORDER BY d.text, d.product_id

给出输出:

product_id|min|date|text
1|8.80|2012-12-19|<-Min price for product 1 on last month
3|2.34|2012-12-24|<-Min price for product 1 on last month
2|3.45|2012-12-02|<-No reg for product 2 on last month, show last reg.
于 2013-01-15T14:42:09.223 回答
-1

不是我测试过,但你可以试试...

SELECT * FROM 
  (SELECT * FROM (
      SELECT * FROM table ORDER BY date DESC)
  as tmp GROUP BY product_id) t1
LEFT JOIN
  (SELECT * FROM (
      SELECT * FROM table WHERE date => CURDATE() ORDER BY price)
  as tmp2 GROUP BY product_id) t2
ON t1.product_id = t2.product_id
于 2013-01-15T14:28:32.903 回答