标签:
CREATE TABLE students
(name varchar(20),/*姓名*/
age int,/*年龄*/
sex varchar(10),/*性别*/
classid int);/*与classes表的classid对应*/
CREATE TABLE classes
(classid int,/*班级ID,与students表中的classid对应*/
name varchar(20),/*班级名称*/
teacher varchar(20));/*班级老师*/
INSERT INTO students (name, age, sex, classid) VALUES (‘刘德华‘, 25, ‘男‘, 1);
INSERT INTO students (name, age, sex, classid) VALUES (‘张惠妹‘, 30, ‘女‘, 1);
INSERT INTO students (name, age, sex, classid) VALUES (‘马艳丽‘, 24, ‘女‘, 2);
INSERT INTO students (name, age, sex, classid) VALUES (‘苍井空‘, 26, ‘女‘, 2);
INSERT INTO students (name, age, sex, classid) VALUES (‘萧敬腾‘, 21, ‘男‘, 1);
INSERT INTO students (name, age, sex, classid) VALUES (‘罗志祥‘, 22, ‘男‘, 3);
INSERT INTO students (name, age, sex, classid) VALUES (‘饭岛爱‘, 23, ‘女‘, 3);
INSERT INTO students (name, age, sex, classid) VALUES (‘周润发‘, 25, ‘男‘, 3);
INSERT INTO students (name, age, sex, classid) VALUES (‘章子怡‘, 20, ‘女‘, 2);
INSERT INTO students (name, age, sex, classid) VALUES (‘陈冠希‘, 22, ‘男‘, 1);
INSERT INTO classes (classid, name, teacher) VALUES (1, ‘C++班‘, ‘王老师‘);
INSERT INTO classes (classid, name, teacher) VALUES (2, ‘IOS班‘, ‘李老师‘);
INSERT INTO classes (classid, name, teacher) VALUES (3, ‘PHP班‘, ‘张老师‘);
select a.name, a.age, a.classid, b.teacher from students a, classes b
where a.classid = b.classid and a.sex = ‘男‘ and a.age > (select avg(age) from students);
select a.name, a.age, a.classid, b.teacher from students a, classes b
where a.classid = b.classid and a.sex = ‘女‘ and a.classid = (select classid from
(select count(*) count, classid from students group by classid) c order by count desc limit 0,1);
标签:
原文地址:http://www.cnblogs.com/ZhangJinkun/p/4570554.html