我正在寻找以下案例的解决方案。让我们假设以下结构给出了数据:
CREATE TABLE IF NOT EXISTS `customer_prices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer` int(11) unsigned NOT NULL,
`price` decimal(8,2) unsigned DEFAULT NULL,
`date` date NOT NULL DEFAULT '0000-00-00',
PRIMARY KEY (`id`),
UNIQUE KEY `item_id_period` (`customer`,`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;
INSERT INTO `customer_prices` (`id`, `customer`, `price`, `date`) VALUES
(131739296, 1, 12.56, '2018-01-01'),
(131739297, 1, 18.33, '2018-01-02'),
(131739301, 1, 15.66, '2018-01-03'),
(131739302, 1, 35.40, '2018-01-04'),
(131739303, 1, 16.96, '2018-01-05'),
(131739304, 1, 11.90, '2018-01-06'),
(131739305, 1, 17.31, '2018-01-07'),
(131739306, 1, 15.75, '2018-01-08');
执行以下查询
SELECT
AVG(price) + AVG(price) / 100 * 10 AS maxprice,
AVG(price) - AVG(price) / 100 * 10 AS minprice,
GROUP_CONCAT(price)
FROM customer_prices
WHERE date > '2018-01-01' AND date < '2018-01-07'
将生成价格清单
18.33, 15.66, 35.40, 16.96, 11.90
我想要的是将这个清单价格限制在minprice和maxprice之间。
我试图这样做,但给了我错误。
SELECT
GROUP_CONCAT(price)
FROM customer_prices
WHERE date > '2018-01-01' AND date < '2018-01-07'
AND price > (AVG(price) - AVG(price) / 100 * 10)
AND price < (AVG(price) + AVG(price) / 100 * 10)
这有解决方法吗?谢谢!