Appearance
MySQL
取中值
这是一种思路,通过 GROUP_CONCAT
把多个数值按顺序组合成字符串,再通过 SUBSTRING_INDEX
取出中间的值。
sql
CREATE TABLE `yager`.`prices` (
`product_id` int NOT NULL,
`price` int NOT NULL
);
sql
select r.product_id,
SUBSTRING_INDEX(SUBSTRING_INDEX(r.prices, ',', CEIL(r.total/2)), ',', '-1') AS price_mid
from (
select GROUP_CONCAT(price ORDER BY price) as prices,
product_id,
COUNT(*) as total
from `prices`
group by `product_id`)
as `r`;