A multi-table query the database
- classification
The combined result set union union all
Join query
En
Outer join
Left outside
Right outside
Natural connection
Subqueries
- The combined result set, the query result is two select statements merged together
Requires two results are combined, and the number of columns must be the same type of column
select * from a union select id,name from b;
select * from a union all select id,name from b;
- Join query (***)
Join query is to compute the product of a plurality of tables, a and b tables, the query result is connected to a * b
select * from stuj,score;
Primary foreign key relationships may be used to remove unnecessary information equation
Join query will produce a Cartesian product
select * from stuj,score where stuj.id=score.id;
- Internal link: link within the above statement is the statement, but not the standard way
sql standards in the link:
select * from stuj inner join score on stuj.id=score.id;
Features: Query result must satisfy the condition of Equation
- External link
The possibility of query results may occur conditions are not satisfied: Features
Left outside: to the left of the main table table
select * from stuj as s left outer join score c on s.id = c.id;
select * from score as s left outer join stuj c on s.id = c.id;
Outer right: the right of the main table to table
- Natural connection
Not need to give the primary foreign key relationship equation, it will automatically find the
select * from score natural join stuj;
- Subquery (****)
A select statement contains another select statement
Also called nested query
Features:
According to the position of sub-query:
After where: Conditions
After from: to do list
When a subquery appears in the where, you can use the following keyword
any
all
The results in the form of sub-queries
Single separate: Conditions
Single line multi-column: Conditions
Multi-line single: Conditions
Rows and columns: Table
- Query paid more than the person's information xiaosan
- Query xiaosan wages
select salary from emp where name=’xiaosan’;
select * from emp where salary>( select salary from emp where name=’xiaosan’);
- Query paid more than 1 greater than the number of people of all the information department
No. --1 largest sector wages
select max(salary) from emp where deptid=1;
- Query No. 1002 of the person's name, salary, department and department address
select name,salary,deptid from emp e where id=1002;
select e.name,e.salary,e.deptid,d.address from emp e,dept d
where e.deptid = d.deptid and id=1002;
select e.name,e.salary,e.deptid,d.address from (select id,salary,deptid from emp) e,dept d where e.deptid = d.deptid and id=1002;
- Connect to their own self-connection
- Query No. 1004 of the names and numbers leadership and leadership Name
select e1.id,e1.name,e2.id,e2.name from emp e1,emp e2
where e1.leader = e2.id and e1.id=1004;