当前位置:网站首页>[MySQL] multi table query
[MySQL] multi table query
2022-07-06 08:47:00 【Charming】
List of articles
Multi-table query
Multi-table query , Also known as associative queries , It means that two or more tables complete the query operation together .
Prerequisite : There is a relationship between these tables queried together ( one-on-one 、 One to many ), There must be associated fields between them , This associated field may establish a foreign key , Or you may not have established a foreign key . such as : Employee table and department table , These two tables depend on “ Department number ” Association .
1. Multi table connection caused by a case
1.1 Case description

- Check the name of the employee ’Abel’ Which city do people work in ?
SELECT *
FROM employees
WHERE last_name = 'Abel';
SELECT *
FROM departments
WHERE department_id = 80;
SELECT *
FROM locations
WHERE location_id = 2500;
Getting data from multiple tables :

Case study : Query the employee's name and department name
SELECT last_name, department_name
FROM employees, departments;

- Query results :
+-----------+----------------------+
| last_name | department_name |
+-----------+----------------------+
| King | Administration |
| King | Marketing |
| King | Purchasing |
| King | Human Resources |
| King | Shipping |
| King | IT |
| King | Public Relations |
| King | Sales |
| King | Executive |
| King | Finance |
| King | Accounting |
| King | Treasury |
...
| Gietz | IT Support |
| Gietz | NOC |
| Gietz | IT Helpdesk |
| Gietz | Government Sales |
| Gietz | Retail Sales |
| Gietz | Recruiting |
| Gietz | Payroll |
+-----------+----------------------+
2889 rows in set (0.01 sec)
- Analyze the error :
SELECT COUNT(employee_id) FROM employees;
# Output 107 That's ok
SELECT COUNT(department_id)FROM departments;
# Output 27 That's ok
SELECT 107*27 FROM dual;
We call the problem in the above multi table query as : The error of Cartesian product .
- There is a Cartesian product error , Reason for the error : The connection condition of multiple tables is missing
# Wrong implementation : Each employee matched with each department .
SELECT employee_id,department_name
FROM employees,departments; # Query out 2889 Bar record
# Wrong way
SELECT employee_id,department_name
FROM employees CROSS JOIN departments;# Query out 2889 Bar record
1.2 The cartesian product ( Or cross connect ) The understanding of the
- Cartesian product is a mathematical operation . Suppose there are two sets X and Y, that X and Y The Cartesian product of is X and Y All possible combinations of , That is, the first object comes from X, The second object comes from Y All the possibilities of . The number of combinations is the product of the number of elements in the two sets .

SQL92 in , Cartesian product is also called
Cross connect, English isCROSS JOIN. stay SQL99 It is also used in CROSS JOIN Means cross connect . Its function is to connect any table , Even if the two tables are not related . stay MySQL Cartesian product will appear in the following cases :Query employee name and department name
SELECT last_name,department_name FROM employees,departments;
SELECT last_name,department_name FROM employees CROSS JOIN departments;
SELECT last_name,department_name FROM employees INNER JOIN departments;
SELECT last_name,department_name FROM employees JOIN departments;
1.3 Case analysis and problem solving
The error of Cartesian product will occur under the following conditions :
- Omit join conditions for multiple tables ( Or related conditions )
- Connection condition ( Or related conditions ) Invalid
- All rows in all tables are interconnected
To avoid Cartesian product , Sure stay WHERE Add a valid connection condition .
After adding connection conditions , The query syntax :
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2; # Connection condition
stay WHERE Clause .
Write it correctly :
Case study : Query the employee's name and department name
SELECT last_name, department_name
FROM employees, departments
WHERE employees.department_id = departments.department_id;
When there are the same columns in the table , Prefix the column name with the table name .
If there are fields in multiple tables in the query statement , You must specify the table where this field is located .
SELECT employees.employee_id,departments.department_name,employees.department_id
FROM employees,departments
WHERE employees.`department_id = departments.department_id;
Suggest : from sql Optimization angle , It is recommended to query multiple tables , Each field is preceded by the table in which it is located .
You can start the watch Alias , stay
SELECTandWHEREAliases for tables used in .
SELECT emp.employee_id,dept.department_name,emp.department_id
FROM employees emp,departments dept
WHERE emp.department_id = dept.department_id;
- If you alias a table , Once in a SELECT or WHERE If table names are used in , You must use the alias of the table , Instead of using the original name of the table .
# The following operation is wrong :
SELECT emp.employee_id,departments.department_name,emp.department_id
FROM employees emp,departments dept
WHERE emp.department_id = departments.department_id;
- Conclusion : If there is
nMultiple tables realize the query of multiple tables , At leastn-1Connection conditions - practice : 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_id
AND d.location_id = l.location_id;
边栏推荐
- Unified ordering background interface product description Chinese garbled
- Revit 二次开发 HOF 方式调用transaction
- Deep analysis of C language pointer
- [NVIDIA development board] FAQ (updated from time to time)
- Mobile phones and computers on the same LAN access each other, IIS settings
- 项目连接数据库遇到的问题及解决
- Deep anatomy of C language -- C language keywords
- Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
- Revit secondary development Hof method calls transaction
- Purpose of computer F1-F12
猜你喜欢

Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school

pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof

Visual implementation and inspection of visdom

可变长参数

Image,cv2读取图片的numpy数组的转换和尺寸resize变化

Generator parameters incoming parameters

Problems in loading and saving pytorch trained models

Charging interface docking tutorial of enterprise and micro service provider platform

优秀的软件测试人员,都具备这些能力

vb. Net changes with the window, scales the size of the control and maintains its relative position
随机推荐
Browser thread
Chrome浏览器的crash问题
Mobile phones and computers on the same LAN access each other, IIS settings
sublime text的编写程序时的Tab和空格缩进问题
PC easy to use essential software (used)
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
可变长参数
vb. Net changes with the window, scales the size of the control and maintains its relative position
MYSQL卸载方法与安装方法
Charging interface docking tutorial of enterprise and micro service provider platform
704 二分查找
C语言双指针——经典题型
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
UnsupportedOperationException异常
个人电脑好用必备软件(使用过)
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
View computer devices in LAN
LeetCode:214. 最短回文串
TDengine 社区问题双周精选 | 第三期
Research and investment forecast report of citronellol industry in China (2022 Edition)