学习Mysql自定义变量
– Mysql分组中位数例子:求每组年龄中位数
– 建表语句
CREATE TABLE es(
id INT,
name
VARCHAR(50),
age INT,
group
INT
)
– 插入数据
INSERT INTO es VALUES(1,‘Tom’,23,1);
INSERT INTO es VALUES(2,‘Kio’,26,2);
INSERT INTO es VALUES(3,‘Jaam’,18,1);
INSERT INTO es VALUES(4,‘Len’,27,2);
INSERT INTO es VALUES(5,‘Hnnh’,33,1);
INSERT INTO es VALUES(6,‘Sche’,20,2);
INSERT INTO es VALUES(7,‘Kaxi’,28,2);
INSERT INTO es VALUES(8,‘Taxi’,32,1);
SELECT * FROM es;
– 第一步筛选:
SELECT id,name
,age
,group
FROM es GROUP BY group
,age DESC
– 第二步筛选