Advanced # 6: join query
/ *
Meaning: also known as multi-table queries, when the field queries from multiple tables, queries connection will be used
Cartesian product phenomenon: m rows in Table 1, there are n rows in Table 2, the results of row = m * n
The reason occurred: There is no valid connection conditions
How to avoid: Add a valid connection conditions
classification:
By Year:
SQL92 standard: only the inner connection
sql99 standard [Recommended]: En + support outer joins (left outer and right outer) + cross-connect
by function:
en:
equijoins
non-equivalent connections
from the connection
outside connection:
left outer
and right outer join
full outer join
cross connection
*/
SELECT * FROM beauty;
SELECT * FROM boys;
SELECT NAME,boyName FROM boys,beauty
WHERE beauty.boyfriend_id= boys.id;
# A, SQL92 standard
# 1, the equivalent connection
/ *
The results in Table ① Multi equivalent connection for the intersection table is part of the multi
②n connection table, at least n-1 connections conditions
③ polyepitopic order is not
④ generally required starting alias table
⑤ previously described can be used with all clause such as sorting, grouping, filtering
*/
Case # 1: Query goddess name and the corresponding male name of God
the SELECT NAME, boyName
the FROM Boys, Beauty
the WHERE beauty.boyfriend_id = boys.id;
Case # 2: Query employee name and the corresponding department name
SELECT last_name,department_name
FROM employees,departments
WHERE employees.`department_id`=departments.`department_id`;
# 2, table aliases
/ *
① increase concise statement of the
field of the same name to distinguish between multiple ②
Note: If the table from the alias, field queries can not use the original name of the table to define
* /
# Query employee names, jobs number, name trades
SELECT e.last_name,e.job_id,j.job_title
FROM employees e,jobs j
WHERE e.`job_id`=j.`job_id`;
# 3, the order may be reversed if the two tables
# Query employee names, jobs number, name trades
SELECT e.last_name,e.job_id,j.job_title
FROM jobs j,employees e
WHERE e.`job_id`=j.`job_id`;
# 4, you can add filters
Case #: Query There were staff bonuses, department name
SELECT last_name,department_name,commission_pct
FROM employees e,departments d
WHERE e.`department_id`=d.`department_id`
AND e.`commission_pct` IS NOT NULL;
Case # 2: Query city name in the second character for the department name and the city name o
SELECT department_name,city
FROM departments d,locations l
WHERE d.`location_id` = l.`location_id`
AND city LIKE '_o%';
# 5, you can add grouping
Case # 1: the number of queries each city department
SELECT COUNT(*) 个数,city
FROM departments d,locations l
WHERE d.`location_id`=l.`location_id`
GROUP BY city;
Case # 2: No inquiries have led the minimum wage and the sector's department name and department of each department of the bonus
the SELECT department_name, d.`manager_id`, MIN (salary)
the FROM the Departments d, the Employees E
the WHERE d.`department_id` = e.`department_id`
the AND a commission_pct the IS the NOT NULL
the GROUP BY DEPARTMENT_NAME, d.`manager_id`;
#. 6, can be added to sort
Case #: query name and the number of jobs of employees in each trade, and in descending order according to the number of employees
SELECT job_title,COUNT(*)
FROM employees e,jobs j
WHERE e.`job_id`=j.`job_id`
GROUP BY job_title
ORDER BY COUNT(*) DESC;
# 7, can achieve three-table joins?
Case #: inquiry staff name, department name and location of the city
SELECT last_name,department_name,city
FROM employees e,departments d,locations l
WHERE e.`department_id`=d.`department_id`
AND d.`location_id`=l.`location_id`
AND city LIKE 's%'
ORDER BY department_name DESC;
# 2, non-equivalent connection
Case # 1: Query wages and wage levels
SELECT salary,grade_level
FROM employees e,job_grades g
WHERE salary BETWEEN g.`lowest_sal` AND g.`highest_sal`
AND g.`grade_level`='A';
/*
select salary,employee_id from employees;
select * from job_grades;
CREATE TABLE job_grades
(grade_level VARCHAR(3),
lowest_sal int,
highest_sal int);
INSERT INTO job_grades
VALUES ('A', 1000, 2999);
INSERT INTO job_grades
VALUES ('B', 3000, 5999);
INSERT INTO job_grades
VALUES('C', 6000, 9999);
INSERT INTO job_grades
VALUES('D', 10000, 14999);
INSERT INTO job_grades
VALUES('E', 15000, 24999);
INSERT INTO job_grades
VALUES('F', 25000, 40000);
*/
# 3, since the connection
Case #: employee name query name and superiors
SELECT e.employee_id,e.last_name,m.employee_id,m.last_name
FROM employees e,employees m
WHERE e.`manager_id`=m.`employee_id`;