当前位置:网站首页>MySQL multi table query
MySQL multi table query
2022-07-28 05:58:00 【Big data management】
**mysql Multi-table query **
# How to realize multi table query
SELECT employee_id,department_name
FROM employees,departments
# I found out 2889 Bar record
SELECT * FROM employees; # Query out 107 Bar record
SELECT 2889/107
FROM DUAL; # The result is 27
SELECT * FROM departments; # You can find that there are 27 Departments
# The correct way to query multiple tables : There should be connection conditions for multiple tables
SELECT employee_id,department_name,employees.department_id
FROM employees,departments
WHERE employees.department_id=departments.department_id;
# Check the employee's employee_id,last_name,department_name,city
SELECT e.employee_id,e.last_name,d.department_name,l.city
FROM employees e,departments d,locations l
WHERE e.department_id=d.department_idAND d.location_id=l.location_id;
# Examples of non equivalent connections
SELECT *
FROM job_grades;
# Query the employee's name and the name of its manager
SELECT emp.employee_id,emp.last_name,mgr.employee_id,mgr.last_name
FROM employees emp,employees mgr
WHERE emp.manager_id=mgr.employee_id;
# practice : Check the information of all employees last_name,department_name Information
SELECT employee_id,department_name
FROM employees,departments
WHERE employees.department_id=departments.department_id;
#SQL92 The syntax implements the inner connection as above
#sql92 Realize external connection in mysql Can't run on , have access to oracle To achieve the following statement
#SQL92 The syntax implements the left outer connection : Use on the right + as follows
SELECT employee_id,department_name
FROM employees,departments
WHERE employees.department_id=departments.department_id(+);
#SQL92 The syntax implements the right outer connection : Use on the left + as follows
SELECT employee_id,department_name
FROM employees,departments
WHERE employees.department_id(+)=departments.department_id;
#sql99 Syntax to implement inner connection
SELECT last_name,department_name
FROM employees e
JOIN departments d
ON e.department_id=d.department_id;
SELECT last_name,department_name,city
FROM employees e
JOIN departments d
ON e.department_id=d.department_id
JOIN locations l
ON d.location_id=l.location_id;
SELECT last_name,department_name
FROM employees e
JOIN departments d
ON e.department_id=d.department_id
JOIN locations l
ON d.location_id=l.location_id
#sql99 Syntax implementation external connection +
# The left outer join
SELECT last_name,department_name
FROM employees e
LEFT OUTER JOIN departments d
ON e.department_id=d.department_id;
# Right connection
SELECT last_name,department_name
FROM employees e
RIGHT OUTER JOIN departments d
ON e.department_id=d.department_id;
# Various connections
# Internal connection
SELECT employee_id,last_name,department_name
FROM employees e
JOIN departments d
ON e.department_id=d.department_id;
# The left outer join
SELECT employee_id,last_name,department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id=d.department_id;
# Right connection
SELECT employee_id,last_name,department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id=d.department_id;
# Only on the left : Use the fields of the table on the right is null
SELECT employee_id,last_name,department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id=d.department_id
WHERE d.department_id IS NULL;
# Only the right : Use the fields of the table on the left is null
SELECT employee_id,last_name,department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id=d.department_id
WHERE e.department_idIS NULL;
# Full outer join : Left outer link union all There is only the right
SELECT employee_id,last_name,department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id=d.department_id
UNION ALL
SELECT employee_id,last_name,department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id=d.department_id
WHERE e.department_idIS NULL;
# Full outer join : Right outer company union all There is only left
SELECT employee_id,last_name,department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id=d.department_id
UNION ALL
SELECT employee_id,last_name,department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id=d.department_id
WHERE d.department_idIS NULL;
#sqL 99 New characteristics : Natural join (natural join) , Generally do not use , It must meet the equivalent content of the same field before it is displayed
SELECT last_name,department_name
FROM employees e
NATURAL JOIN departments d
#sqL 99 New characteristics :using join
SELECT last_name,department_name
FROM employees e
JOIN departments d
ON e.department_id=d.department_id;
边栏推荐
猜你喜欢
随机推荐
MySQL练习题50道+答案
Help document making based on easy CHM and vs
书籍-邓普顿教你逆向
(php毕业设计)基于php小说网站管理系统获取
数字藏品“大乱斗”,千亿市场变局将至?
路由器与交换机的区别
Mars数*字*藏*品*平*台守卫者计划细节公布
撞脸ins? Mars数字藏品平台探索数字藏品轻社交
Books - investment ideas and Strategies
tcp和udp的区别和联系
连续登陆问题
文件上传漏洞总结
Acquisition of mental health service system based on PHP (PHP graduation design)
uniapp问题:“navigationBarTextStyle“报错:Invalid prop: custom validator check failed for prop “navigat
[interview question] anti shake and throttling
Model builder of ArcGIS
Apache Log4j任意代码执行复现
出游不易,看景区用数字藏品刷存在感
js-宏任务和微任务
登录时密码错误次数过多,对该用户进行封禁,









