MYSQL选择最接近日期的记录

时间:2018-07-17 05:30:12

标签: mysql date select records closest

我的数据库中有以下2个表:

product_price

CREATE TABLE `product_price` (
  `asin` varchar(10) NOT NULL,
  `date` date NOT NULL,
  `price` decimal(7,2) NOT NULL DEFAULT '0.00',
  PRIMARY KEY (`asin`,`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

product_info

CREATE TABLE `product_info` (
  `asin` varchar(10) NOT NULL,
  `name` varchar(200) DEFAULT NULL,
  `brand` varchar(50) DEFAULT NULL,
  `part_number` varchar(50) DEFAULT NULL,
  `url` text,
  `image` text,
  PRIMARY KEY (`asin`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

我想找到在开始日期和结束日期之间价格已经降低的产品。

我现在正在使用以下查询,该查询有效:

$query      = "SELECT pi.*, prev.price AS 'old_price', curr.price, ROUND((100.0*(curr.price - prev.price) / prev.price),0) As PercentDiff FROM product_price As curr 
       JOIN product_price As prev ON curr.date = '".$end_date."' AND prev.date = '".$start_date."' 
       JOIN product_info pi ON curr.asin = pi.asin WHERE curr.asin = prev.asin HAVING PercentDiff < 0 ORDER BY PercentDiff";

但是问题是,如果价格未更改,则表product_price没有记录。

例如

asin        date         price
AAAAAAAAA  2018-07-17     7
AAAAAAAAA  2018-07-15     6

start_date =“ 2018-07-16” end_date =“ 2018-07-17”

由于价格降低,我希望它能出现在我的结果中。

我如何更改查询?

我尝试将prev.date ='“。$ start_date。”'更改为prev.date <='“。$ start_date。”'

但是返回所有记录,而不是我想要的最接近的记录。

以下是来自product_price的一些示例数据:

+------------+------------+--------+
| asin       | date       | price  |
+------------+------------+--------+
| B000GBKDB4 | 2018-07-02 |  38.66 |
| B000GBKDCI | 2018-07-02 |  72.98 |
| B000GBKFLW | 2018-07-02 |  33.27 |
| B000GBKFMG | 2018-07-02 |  63.45 |
| B000GBLZEI | 2018-07-02 |  34.90 |
| B000GBLZEI | 2018-07-04 |  21.31 |
| B000GBLZEI | 2018-07-05 |  20.24 |
| B000GBLZEI | 2018-07-06 |  18.41 |
| B000GBLZEI | 2018-07-08 |  17.49 |
| B000GBLZEI | 2018-07-09 |  15.79 |
| B000GBLZEI | 2018-07-11 |  14.84 |
| B000GBLZEI | 2018-07-16 |  14.29 |

1 个答案:

答案 0 :(得分:1)

仍然不太清楚您的要求,但这应该选择适用于一对日期的正确价格。不会出现在product_price表中的2018-07-03至2018-07-07。

希望这会让您走上正确的轨道。

SELECT *,

    (SELECT price 
        FROM product_price 
        WHERE product_price.asin = product_info.asin AND 
            product_price.date <= '2018-07-03'
        ORDER BY product_price.date DESC
        LIMIT 1) AS start_price,

    (SELECT price 
        FROM product_price 
        WHERE product_price.asin = product_info.asin AND 
            product_price.date <= '2018-07-07'
        ORDER BY product_price.date DESC
        LIMIT 1) AS end_price

FROM product_info
WHERE asin = 'B000GBLZEI'

在评论之后,您可以使用这样的临时表

CREATE TEMPORARY TABLE IF NOT EXISTS price_changes (
    asin VARCHAR(10), 
    start_price DECIMAL(7,2), 
    end_price DECIMAL(7,2));

TRUNCATE price_changes;

INSERT INTO price_changes 
SELECT asin, 

    (SELECT price 
        FROM product_price 
        WHERE product_price.asin = product_info.asin AND 
            product_price.date <= '2018-07-03'
        ORDER BY product_price.date DESC
        LIMIT 1) AS start_price,

    (SELECT price 
        FROM product_price 
        WHERE product_price.asin = product_info.asin AND 
            product_price.date <= '2018-07-07'
        ORDER BY product_price.date DESC
        LIMIT 1) AS end_price

FROM product_info;

SELECT *,
    ROUND((100.0 * (end_price - start_price) / start_price), 0) AS price_difference
FROM product_info 
    JOIN price_changes ON
        price_changes.asin = product_info.asin;