我想写mysql查询

时间:2012-04-17 18:06:06

标签: mysql

打印级别和该级别学生的平均年龄,除了JR以外的所有级别。

CREATE TABLE STUDENT (
    sid NUMERIC(6) NOT NULL,
    sname VARCHAR(15),
    major VARCHAR(20),
    level VARCHAR(2),
    sage NUMERIC(2) NOT NULL,
    PRIMARY KEY (sid)
);

INSERT INTO STUDENT (sid, sname, major, level, sage) values (1,'JOHN','EE','FR' ,18);
INSERT INTO STUDENT (sid, sname, major, level, sage) values (2,'TIM','EE','FR',19);
INSERT INTO STUDENT (sid, sname, major, level, sage) values (3,'RICHARD','EE','SO',20);
INSERT INTO STUDENT (sid, sname, major, level, sage) values (4,'EDWARD','EE','SO',21);
INSERT INTO STUDENT (sid, sname, major, level, sage) values (5,'ALBER','CS','JR',22);
INSERT INTO STUDENT (sid, sname, major, level, sage) values (6,'MARY','EE','JR',22);
INSERT INTO STUDENT (sid, sname, major, level, sage) values (7,'JACK','EE','SR',23);
INSERT INTO STUDENT (sid, sname, major, level, sage) values (8,'JULIAN','EE','SR',22);
INSERT INTO STUDENT (sid, sname, major, level, sage) values (9,'SAM','EE','SR',24);
INSERT INTO STUDENT (sid, sname, major, level, sage) values (10,'RAM','CS','SR',23);
INSERT INTO STUDENT (sid, sname, major, level, sage) values (11,'RICK','EE','SR',24);

2 个答案:

答案 0 :(得分:2)

  SELECT level, AVG(sage) AS AvgAge
    FROM STUDENT
   WHERE level != 'JR'
GROUP BY level

答案 1 :(得分:0)

SELECT level, AVG(sage) 
    FROM STUDENT
    WHERE level <> 'JR' 
    GROUP BY level