MySQL 计算中位数
mysql中位数 奇数取中间的值,偶数取中间两个数的平均值
eg: 12345 中位数4 1234 中位数2.5
SELECT avg(t1.money) as median_val FROM (
SELECT @rownum:=@rownum+1 as row_number
, d.money
FROM core_order d, (SELECT @rownum:=0) r
WHERE 1
– (条件)
ORDER BY d.money
) as t1,
(
SELECT count(*) as total_rows
FROM core_order d
WHERE 1
– (条件)
) as t2
WHERE 1
AND t1.row_number in ( floor((total_rows+1)/2), floor((total_rows+2)/2) );