MySQL table join (use of internal and external joins and renaming)

#to practice more

 

1. The connection query is divided into

inner join

   Equijoin

   non-equijoin

   self-connection

outer join

    Left outer join (left join)

   Right outer join (right join)

 

When multiple tables are connected to query, if there are no conditions to limit, the query will be

What happened?

There will be a Cartesian product, that is, the product of the number of records of the two tables

 

##Where to use as? Why can't use as here?

 

#sql92: Equijoin in inner join

select e.ename,d.dname 

from emp e,dept d

where e.deptno=d.deptno;

 

#SQL99: Equijoin in inner join, advantage: If you are not satisfied with table join,

#You can add where to filter.

select e.ename,d.dname 

from emp e join dept d

on e.deptno=d.deptno;

 

#SQL99: Non-equijoin in inner join?

#eg: Display salary grade, department name, salary, etc.

select e.ename,e.sal,s.grade

from emp e join salgrade s

ten e.sal

between s.losal and s.hisal;

 

#Find out the name of each employee's superior leader, indicating that the leader is also subordinate to the employee table, so it is necessary to

# Treat a table as two tables (a and b).

select a.ename empname,b.ename

leadrname from emp a inner join

emp b on a.mgr=b.empno;

select a.ename empname,b.ename 

leadername from emp a join emp b

on a.mgr=b.empno;

 

#Find out the department name corresponding to each employee and ask to display all

#Display all department names

select e.ename,d.dname 

from emp e 

right join dept d 

on d.deptno=d.deptno;

 

#When to use outer join? When to use inner join?

#The number of records in the inner connection is less than or equal to the outer connection, the inner connection

#Yes, matching information will be displayed, and incomplete information will not be displayed

#And the outer connection (unconditional display of all) will display all

#Left connection is to display all the left side, while right connection is to display all the right side

#Display, if there is no record, use null to match

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324522969&siteId=291194637