当前位置:网站首页>[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 nameDistinguish .
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
table1andtable2It'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.
边栏推荐
- Druids connect to mysql8.0.11
- How to quickly build high availability of service discovery
- MLX90614 driver, function introduction and PEC verification
- Op amp related - link
- How to write a good title of 10w+?
- Learning methods of zynq
- Is the controller a single instance or multiple instances? How to ensure the safety of concurrency
- Common mode interference of EMC
- Mongoose the table associated with the primary key, and automatically bring out the data of another table
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
猜你喜欢

Scratch uses runner Py run or debug crawler

Sort merge sort

Cgb2201 preparatory class evening self-study and lecture content

Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities

2022 free examination questions for hoisting machinery command and hoisting machinery command theory examination

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

How can enterprises and developers take advantage of the explosion of cloud native landing?

Recursive least square adjustment
![[note] IPC traditional interprocess communication and binder interprocess communication principle](/img/f6/36c28df02198539e27352e3cdf4ba6.jpg)
[note] IPC traditional interprocess communication and binder interprocess communication principle

Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
随机推荐
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Gorilla/mux framework (RK boot): add tracing Middleware
Leetcode: a single element in an ordered array
C3p0 connection MySQL 8.0.11 configuration problem
"Learning notes" recursive & recursive
Common mode interference of EMC
Qtoolbutton available signal
How to prevent malicious crawling of information by one-to-one live broadcast source server
What are the common computer problems and solutions
Learning notes of raspberry pie 4B - IO communication (SPI)
Arc135 partial solution
ADB related commands
Go error collection | talk about the difference between the value type and pointer type of the method receiver
URLEncoder. Encode and urldecoder Decode processing URL
C deep anatomy - the concept of keywords and variables # dry inventory #
Druids connect to mysql8.0.11
File copy method
Fashion cloud interview questions series - JS high-frequency handwritten code questions
Interesting 10 CMD commands
Current detection circuit - including op amp current scheme