Drop table if not exists employees;
create table employees(id int primary key auto_increment,namevarchar(50)
,salary int,managerid int references employees(id));
insert into employees values (null,' lhm',10000,null), (null,'zxx',15000,1
),(null,'flx',9000,1),(null,'tg',10000,2),(null,'wzg',10000,3);
Wzg大于flx,lhm大于zxx
解题思路:
根据sql语句的查询特点,是逐行进行运算,不可能两行同时参与运算。
涉及了员工薪水和经理薪水,所有,一行记录要同时包含两个薪水,所有想到要把这个表自关联组合一下。
首先要组合出一个包含有各个员工及该员工的经理信息的长记录,譬如,左半部分是员工,右半部分是经理。而迪卡尔积会组合出很多垃圾信息,先去除这些垃圾信息。
select e.* from employees e,employees m where e.managerid=m.id ande.sala
ry>m.salary;
6、求出小于45岁的各个老师所带的大于12岁的学生人数
中有3个表 teacher 表,student表,tea_stu关系表。
teacher 表 teaID name age
student 表 stuID name age
teacher_student表 teaID stuID
要求用一条sql查询出这样的结果
1.显示的字段要有老师name, age 每个老师所带的学生人数
2 只列出老师age为40以下,学生age为12以上的记录
预备知识:
1.sql语句是对每一条记录依次处理,条件为真则执行动作(select,insert,delete,update)
2.只要是迪卡尔积,就会产生“垃圾”信息,所以,只要迪卡尔积了,我们首先就要想到清除“垃圾”信息
实验准备:
drop table if exists tea_stu;
drop table if exists teacher;
drop table if exists student;
create table teacher(teaID int primary key,name varchar(50),ageint);
create table student(stuID int primary key,name varchar(50),ageint);
create table tea_stu(teaID int references teacher(teaID),stuID intreferences student(stuID));
insert into teacher values(1,'zxx',45), (2,'lhm',25) , (3,'wzg',26), (4,'tg',27);
insert into student values(1,'wy',11), (2,'dh',25) , (3,'ysq',26) ,(4,'mxc',27);
insert into tea_stu values(1,1), (1,2), (1,3);
insert into tea_stu values(2,2), (2,3), (2,4);
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-34607-11.html
一会上市
来抱中国大腿就对了
我们国家可以把土地重复利用我们也可以用好多年的积蓄买一套二三线城市的房