Benpian involved data tables can be found on its own chapter "MySQL difficulty grammar - subqueries"
There are three connections between MySQL data table: equivalent connection, external connection, since the connection. The following describes by way of example three connections
1, the equivalent connection
Equijoins generally applicable to the connection of the main table and an outer (primary key and the foreign key table is equal to the outer primary)
Requirements: 1 View the name of the department where all employees
the SELECT s_emp.id staff, last_name employee name, s_dept.id department number, name department name from s_dept, s_emp the WHERE s_dept.id = s_emp.dept_id;
Requirements: 2 query average wage of more than 1200 department, and displays the names of those sectors
select s_dept.id, name from s_emp, s_dept where s_emp.dept_id=s_dept.id group by dept_id having avg(salary) > 1200;
Requirements: 3 viewing area where the names of all employees
select last_name, s_dept.id, s_region.name from s_emp, s_dept, s_region where s_emp.dept_id=s_dept.id and s_dept.region_id=s_region.id
Requirements: 4 to view the employee id, last_name, salary, department name, the name of the region, these employees have the following conditions: salary greater than the average wage in the area where or with Chang Chang employees not in the same departments
the SELECT s_emp.id, last_name, salary, s_dept.name, s_region.name from s_emp, s_dept, s_region the WHERE s_emp.dept_id = s_dept.id and s_dept.region_id = s_region.id and (salary > sector (# chang in your area the average wage select AVG (salary) from s_emp the wHERE dept_id in (# of areas where the department chang select the above mentioned id from s_dept the wHERE region_id = (# Chang area where select region_id from s_emp, s_dept where s_emp.dept_id=s_dept.id and last_name="chang" ) ) or last_name != "chang") );
Requirements: 5 View wages higher than the average wage of employees where the department id and name
the SELECT s_emp.id, last_name from s_emp, s_dept, (the average salary for each department table # the SELECT dept_id, AVG (salary) AS avg_salary from s_emp Group by dept_id) AS avg_table the WHERE s_emp.dept_id = s_dept.id and s_dept.id = avg_table.dept_id # payroll department number and department table joins and salary > avg_salary;
SELECT e.dept_id, n.avg_salary, d.name from s_emp AS E, s_dept AS D, (# sector pay table SELECT the dept_id, AVG (the salary) AS avg_salary from s_emp Group by the dept_id) AS n- WHERE e.dept_id = D. ID and d.id = n.dept_id and n.dept_id ! = 41 # 41 is greater than the sector does not exist and the salary > (# 41 greater than the average salary department SELECT avg_salary from ( # 部门薪资表 select dept_id, avg(salary) as avg_salary from s_emp group by dept_id) as n where dept_id =41)
2, the outer connector
External connections can be divided into two connections: connect the left and right connections
Left connection: Table 1 left join table relationships and table 2 ... on the table, on the ground behind is a condition, not necessarily the equivalence relationship between two tables (Table 1 data are complete)
select c.name, e.last_name from s_customer as c left join s_emp as e # 左连接 on e.id=c.sales_rep_id;
Right connection: Table 1 right jion Table 2 .... ON association table and the table, followed by a contact ON condition, the equivalence relation is not necessarily two tables (Data Table 2 is intact)
Requirements: sales staff to view customer names and their corresponding names
select c.name, e.last_name from s_emp as e right join s_customer as c # 右连接 on e.id=c.sales_rep_id;
3, since the connection
As the name suggests it is to form their own association - an association table a foreign key to its primary key to the employee table have their own leadership, but leadership itself is an employee: examples are as follows
Demand: See all employee names and their corresponding names manager
select id, last_name from s_emp where manager_id = id