+-Many multi-table query three tables even check the sub-queries +

Multi-table query 
query multiple tables of data needed
for instance: there are classes and students table table
you have to consult the class name data for all participants
will check the table to get the class id a class and then go to college based on the id corresponding table query student

prepare data:
Create Table EMP (int ID, name char (10), Sex char, the dept_id int);
INSERT EMP values (. 1, "rhubarb", "m",. 1);
INSERT EMP values (2, "Wang "," m ", 2);
INSERT EMP values (. 3," talk of the town "," W ", 30);

Create Table Dept (ID int, char name (10));
INSERT Dept values (. 1," market " );
INSERT the dept values (2, "financial");
INSERT the dept values (3, "administrative");

the way multi-table queries
1. Cartesian product queries
what is Cartesian product,With a record coordinates to link all the records in another table
like the two tables of data to do a multiplication
which will result in a lot of useless duplication of data
we have to the effect that: employee id and department table the department table spliced together on the same id
Screening with the correct data where
the SELECT * from emp, the dept where emp.dept_id = dept.id;

ON key
role in a multi-table query conditions is
select * from emp, dept on emp.dept_id = dept.id; this is because on the wrong syntax can be used only in special multi-table queries in


connection queries within 2.
inner the Join
the SELECT * from emp inner emp.dept_id on the Join the dept = dept.id;

# query for all employees and they belong Segment information
3. left outer join
left join
left of the data tables is an exact match the data displayed on the right side of the table to display only
the SELECT * from emp left join the dept oN emp.dept_id = dept.id;

# to query all sectors and all their employee information
4. right outer join
right join
the table data matches the left to the right until the display data in the table show complete
select * from emp right join dept on emp.dept_id = dept.id;

# Displays all the data in a plurality of tables Table
5. Full linked
full join mysql oracle support does not support
can be achieved indirectly by Union
Union represents a combined query to a plurality of means merge query results show
requirement is merged table structure must be the same
default deduplication

combined repeated but without removing the
Union All

SELECT * from the Join Dept EMP right emp.dept_id ON = dept.id
Union
SELECT * from the Join Dept EMP left ON emp.dept_id = dept.id;




summary: multi-table Fill in the blanks in writing in accordance with the link to the left if you want to display all written with a left join
all right join with the right
to display all the results and consolidated results of left and right links links
course, you can check together more tables but does not make sense and you should try avoid too many tables to check with
a maximum of three in a many-time

select *from emp left join dept left join xxtable on emp.dept_id = dept.id;

create table tec(id int,name char(10));
insert into tec value(1,"egon");
insert into tec value(2,"yyh");

create table stu(id int,name char(10));
insert into stu value(1,"大傻");
insert into stu value(2,"中傻");
insert into stu value(3,"小傻");
create table s_t(s_id int,t_id int);
insert into s_t value(1,2);
insert into s_t value(2,2);
insert into s_t value(3,1);

需求 找出 yyh 这个老师 教过的学生信息
思路:
第一步 到关系表中 去查询 哪些老师教过哪些学生(学生的id) 形成了一个临时表
第二步 将上一步得到临时表 与 学生表进行连接
第三步 加上额外的筛选条件 老师的name 是 yyh


select tec.name teacher,stu.name student from
tec inner join s_t on tec.id = s_t.t_id
inner join stu on s_t.s_id = stu.id
where tec.name = "egon" ;

子查询
什么是子查询:将上一次查询的结果 作为本次查询的原始数据(或是查询条件)
也就是嵌套查询 这中嵌套的语法一班不让写,不允许使用,

Guess you like

Origin www.cnblogs.com/1832921tongjieducn/p/11128910.html