当前位置:网站首页>[MySQL] classification of multi table queries
[MySQL] classification of multi table queries
2022-07-03 23:19:00 【Charming】
List of articles
Multi table query classification
classification 1: Equivalent connection vs Non equivalent connection
1. Equivalent connection
SELECT employees.employee_id, employees.last_name,
employees.department_id, departments.department_id,
departments.location_id
FROM employees, departments
WHERE employees.department_id = departments.department_id;
expand 1: Multiple connection conditions and AND The operator
expand 2: Distinguish between duplicate column names
- When there are the same columns in multiple tables , The column name must be prefixed with the table name .
- Columns with the same column name in different tables can be used
Table name
Distinguish .
SELECT employees.last_name, departments.department_name,employees.department_id
FROM employees, departments
WHERE employees.department_id = departments.department_id;
expand 3: The table alias
Using aliases can simplify queries .
Using table name prefix before column name can improve query efficiency .
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e , departments d
WHERE e.department_id = d.department_id;
It should be noted that , If we use the alias of the table , In the query field 、 Only aliases can be used in the filter conditions , Cannot use the original table name , Otherwise you will report an error .
Ali Development Specification
:【
mandatory
】 For queries and changes to table records in the database , As long as multiple tables are involved , You need to add the alias of the table before the column name ( or Table name ) To limit .
explain
: Query records for multiple tables 、 Update record 、 When deleting records , If there is no alias of the qualified table for the operation column ( Or watch name ), And when the operation column exists in multiple tables , I'm going to throw an exception .
Example
:select t1.name from table_first as t1 , table_second as t2 where t1.id=t2.id;
Counter example
: In a business , Because the multi table associated query statement does not add the alias of the table ( Or watch name ) The limitation of , Two years after normal operation , Recently Add a field with the same name to a table , After making database changes in the pre release environment , The online query statement appears 1052 abnormal :Column ‘name’ in field list is ambiguous.
expand 4: Join multiple tables
summary : Connect n Tables , Need at least n-1 Connection conditions . such as , Join three tables , At least two join conditions are required .
practice : Query the employee's last_name,department_name, city
SELECT 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
2. Non equivalent connection
SELECT e.last_name, e.salary, j.grade_level
FROM employees e, job_grades j
WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal;
or
WHERE e.salary >= j.lowest_sal AND e.salary <= j.highest_sal;
classification 2: Self join vs Non self connecting
- When
table1
andtable2
It's essentially the same table , Just use the alias to form two virtual tables to represent different meanings . Then the two tables are connected internally , External connection and other queries .
subject : Inquire about employees surface , return “Xxx works for Xxx”
SELECT CONCAT(worker.last_name ,' works for '
, manager.last_name)
FROM employees worker, employees manager
WHERE worker.manager_id = manager.employee_id ;
- Examples of self connection :
Query employees id, The name of the employee and the of his / her manager id And name
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`;
classification 3: Internal connection vs External connection
- In addition to querying the records that meet the conditions , External join can also query records that one party does not meet the conditions .
- Internal connection : Merging rows of more than two tables with the same column , There are no rows in the result set that do not match one table with another
SELECT employee_id,department_name
FROM employees e,departments d
WHERE e.`department_id` = d.department_id; # Only 106 Bar record
External connection : Merging rows of more than two tables with the same column , Except that the result set contains rows that match one table with another , The left table is also queried or Right table Mismatched rows .
Classification of external connections : The left outer join 、 Right connection 、 Full outer join
The left outer join : In addition to the rows that meet the connection conditions, the two tables also return the rows that do not meet the conditions in the left table , This connection is called The left outer join .
Right connection : In addition to the rows that meet the connection conditions, the two tables also return the rows that do not meet the conditions in the right table , This connection is called Right connection .
summary :
Internal connection : Merging rows of more than two tables with the same column , There are no rows in the result set that do not match one table with another .
External connection : In addition to returning rows that meet the connection conditions during the connection of two tables Also return to the left ( Or right ) Rows in the table that do not meet the criteria , This connection is called left ( Or right ) External connection . When there are no matching rows , The corresponding column in the result table is empty (
NULL
).If it's a left outer connection , The table on the left of the join condition is also called
Main table
, The table on the right is calledFrom the table
.If it's a right outer connection , The table on the right in the join condition is also called
Main table
, The table on the left is calledFrom the table
.
边栏推荐
- [note] glide process and source code analysis
- Pan Yueming helps Germany's Rochester Zodiac custom wristwatch
- 2/14 (regular expression, sed streaming editor)
- Qtoolbutton available signal
- A preliminary study on the middleware of script Downloader
- Hcip day 15 notes
- pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
- Recursive least square adjustment
- [15th issue] Tencent PCG background development internship I, II and III (OC)
- Weekly leetcode - nc9/nc56/nc89/nc126/nc69/nc120
猜你喜欢
Fluent learning (4) listview
Cgb2201 preparatory class evening self-study and lecture content
Creation of the template of the password management software keepassdx
Common mode interference of EMC
How the computer flushes the local DNS cache
Programming language (1)
Selenium check box
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Pyqt5 sensitive word detection tool production, operator's Gospel
Qtoolbutton - menu and popup mode
随机推荐
User login function: simple but difficult
Text replacement demo
540. Single element in ordered array
What are the securities companies with the lowest Commission for stock account opening? Would you recommend it? Is it safe to open an account on your mobile phone
SDMU OJ#P19. Stock trading
Bufferpool caching mechanism for executing SQL in MySQL
D29:post Office (post office, translation)
How can enterprises and developers take advantage of the explosion of cloud native landing?
Qtoolbutton - menu and popup mode
Get current JVM data
Hcip day 16 notes
Blue Bridge Cup -- Mason prime
Overview of Yunxi database executor
How to understand the gain bandwidth product operational amplifier gain
C summary of knowledge point definitions, summary notes
Creation of the template of the password management software keepassdx
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg
pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
How to solve win10 black screen with only mouse arrow
Sort merge sort