目录
1 所有有门派的人员信息
2 列出所有用户,并显示其机构信息
3 列出不入派的人员:
4 所有没人入的门派 :
5 列出所有人员和门派的对照关系
6 列出所有没入派的人员和没人入的门派
7 求各个门派对应的掌门人名称:
8 求所有当上掌门人的平均年龄:
9 求所有人物对应的掌门名称:
10 列出自己的掌门比自己年龄小的人员
11 列出所有年龄低于自己门派平均年龄的人员
12 列出至少有2个年龄大于40岁的成员的门派
13 至少有2位非掌门人成员的门派
14 列出全部人员,并增加一列备注“是否为掌门”,如果是掌门人显示是,不是掌门人显示否
15 列出全部门派,并增加一列备注“老鸟or菜鸟”,若门派的平均值年龄>50显示“老鸟”,否则显示“菜鸟”
16 显示每个门派年龄最大的人
表结构:
t_dept
t_emp
创建表:
CREATE TABLE `t_dept` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `deptName` VARCHAR(30) DEFAULT NULL, `address` VARCHAR(40) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; CREATE TABLE `t_emp` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(20) DEFAULT NULL, `age` INT(3) DEFAULT NULL, `deptId` INT(11) DEFAULT NULL, empno int not null, PRIMARY KEY (`id`), KEY `idx_dept_id` (`deptId`) #CONSTRAINT `fk_dept_id` FOREIGN KEY (`deptId`) REFERENCES `t_dept` (`id`) ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
插入数据:
INSERT INTO t_dept(deptName,address) VALUES('华山','华山'); INSERT INTO t_dept(deptName,address) VALUES('丐帮','洛阳'); INSERT INTO t_dept(deptName,address) VALUES('峨眉','峨眉山'); INSERT INTO t_dept(deptName,address) VALUES('武当','武当山'); INSERT INTO t_dept(deptName,address) VALUES('明教','光明顶'); INSERT INTO t_dept(deptName,address) VALUES('少林','少林寺'); INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('风清扬',90,1,100001); INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('岳不群',50,1,100002); INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('令狐冲',24,1,100003); INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('洪七公',70,2,100004); INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('乔峰',35,2,100005); INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('灭绝师太',70,3,100006); INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('周芷若',20,3,100007); INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('张三丰',100,4,100008); INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('张无忌',25,5,100009); INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('韦小宝',18,null,100010); ALTER TABLE `t_dept` add CEO INT(11) ; # CEO=2 值,都应该是t_emp 中id的值。 update t_dept set CEO=2 where id=1; update t_dept set CEO=4 where id=2; update t_dept set CEO=6 where id=3; update t_dept set CEO=8 where id=4; update t_dept set CEO=9 where id=5;
表内容:
查询需求:
select *from t_emp inner join t_dept on t_emp.deptId=t_dept.id;
select *from t_emp left join t_dept on t_emp.deptId=t_dept.id;
select name from t_emp left join t_dept on t_emp.deptId=t_dept.id where t_emp.deptId is NULL;
select *from t_dept left join t_emp on t_emp.deptId=t_dept.id where CEO is null;
select name,deptName from t_dept right join t_emp on t_emp.deptId=t_dept.id;
或者
select name,deptName from t_emp,t_dept where t_emp.deptId=t_dept.id;
select *from t_emp left join t_dept on t_emp.deptId=t_dept.id where t_dept.id is null
union
select *from t_dept left join t_emp on t_emp.deptId=t_dept.id where t_emp.deptId is null;
select name,deptName from t_emp right join t_dept on t_emp.deptId=t_dept.id where t_emp.id=t_dept.CEO;
select avg(age) from t_emp right join t_dept on t_emp.deptId=t_dept.id where t_emp.id=t_dept.CEO;
select ed.name '人物',c.name '掌门' from
(select e.name,d.ceo from t_emp e left join t_dept d on e.deptid=d.id) ed
left join t_emp c on ed.ceo=c.id;
或
select t_emp.name,c.name from t_emp left join (select t_emp.name,t_emp.deptId as id from t_dept left join t_emp on t_emp.id=t_dept.CEO) c on c.id=t_emp.deptId;
select ed.name '人物',c.name '掌门' from
(select e.name,d.ceo from t_emp e left join t_dept d on e.deptid=d.id) ed
left join t_emp c on ed.ceo=c.id
where (select age from t_emp where t_emp.name=c.name)<(select age from t_emp where t_emp.name=ed.name);
select a.name,a.age,c.name ceoName,c.age ceoAge from t_emp a
left join t_dept b on a.deptId=b.id
left join t_emp c on b.CEO=c.id
where a.age>c.age;
SELECT name FROM t_emp,(SELECT AVG(age) as age,deptName,deptId FROM t_emp,t_dept where t_emp.deptId = t_dept.id GROUP BY deptId)a WHERE t_emp.deptId = a.deptId and a.age > t_emp.age;
SELECT AVG(age) as age,deptName,deptId FROM t_emp,t_dept where t_emp.deptId = t_dept.id GROUP BY deptId;
mysql> SELECT deptName FROM t_dept LEFT JOIN t_emp on t_emp.deptId = t_dept.id where t_emp.age > 40 GROUP BY t_dept.deptName HAVING COUNT(0) >= 2;
mysql> select deptName from t_dept left join t_emp on t_emp.deptId = t_dept.id group by t_dept.deptName having count(0)>2;
或
mysql> SELECT deptName,t_dept.id FROM t_dept LEFT JOIN t_emp on t_dept.id = t_emp.deptId and t_dept.CEO <> t_emp.id GROUP BY id HAVING COUNT(0) >=2;
SELECT t_emp.name,CASE WHEN t_dept.id is null THEN '否' ELSE '是' END '是否掌门人' FROM t_emp LEFT JOIN t_dept on t_emp.id = t_dept.ceo;
select t_dept.deptName, case when avg(t_emp.age)>50 then '老鸟' else '菜鸟' end as 备注 from t_dept join t_emp on t_dept.id=t_emp.deptId group by t_dept.deptName;
select t_emp.deptId,max(t_emp.age) max_age from t_emp where t_emp.deptId is not null group by t_emp.deptId;