mysql选择日期最近的记录

zbdgwd5y  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(432)

我的数据库中有以下两个表:
产品价格

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

产品信息

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";

但问题是,如果价格没有变化,表格产品价格就没有记录。
例如

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

开始日期=“2018-07-16”结束日期=“2018-07-17”
我希望它在我的结果,因为价格降低了。
我怎么改变我的问题??
我试图将prev.date='“$start\u date.”更改为prev.date<='“$start\u date.”
但是返回所有的记录,而不是我想要的最接近的记录。
以下是产品价格的一些样本数据:

+------------+------------+--------+
| 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 |
a5g8bdjr

a5g8bdjr1#

仍然不是很清楚你在追求什么,但这应该选择正确的价格适用于一对日期。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;

相关问题