当前位置:网站首页>MySQL connection query
MySQL connection query
2022-06-13 01:20:00 【Torlesse】
Mysql Link query
Database data preparation
-- Create department table
CREATE TABLE dept (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
);
INSERT INTO dept (NAME) VALUES (' Development Department '),(' The Marketing Department '),(' Finance Department ');
-- Create an employee table
CREATE TABLE emp (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(10),
gender CHAR(1), -- Gender
salary DOUBLE, -- Wages
join_date DATE, -- Date of entry
dept_id INT
);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES(' The Monkey King ',' male ',7200,'2013-02-24',1);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES(' Pig eight quit ',' male ',3600,'2010-12-02',2);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES(' Tang's monk ',' male ',9000,'2008-08-08',2);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES(' Bones jing ',' Woman ',5000,'2015-10-07',3);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES(' Spider essence ',' Woman ',4500,'2011-03-14',1);
Internal connection
Implicit inner join
# Implicit inner join
SELECT * FROM emp,dept WHERE emp.id = dept.id;

Explicit inner connection
# Explicit inner connection
SELECT * FROM emp INNER JOIN dept ON emp.id = dept.id;

Summary :
- The internal connection query is the public part , Meet the connection conditions ( Master foreign key relationship ) Part of
- Use the records of the left table to match the records of the right table , If the conditions are met, then
External connection
The left outer join
# The left outer join
SELECT * FROM emp LEFT JOIN dept ON emp.id = dept.id;

Right connection
# Right connection
SELECT * FROM emp RIGHT JOIN dept ON emp.id = dept.id;

Summary :
- The left outer join : On the basis of inner connection, ensure that all the data in the left table are displayed
- Right connection : On the basis of inner connection, ensure that all the data in the table on the right are displayed
Subquery
What is subquery ?
A query contains at least 2 individual select
Three cases of sub query results
The subquery result is a value
problem 1: Find out who the highest paid employees are
decompose : Query maximum wage
SELECT MAX(salary) FROM emp;

Check salary is 9000 The employees' ( That is to query the highest paid employees )
SELECT * FROM emp WHERE salary=(SELECT MAX(salary) FROM emp);

When the sub query result is single column and multiple rows
problem 1: Query the employees whose salary is less than the average salary
decompose : Query average wage
SELECT AVG(salary) FROM emp;

Query employees whose salary is less than the average salary
SELECT * FROM emp WHERE salary < (SELECT AVG(salary) FROM emp);

The result of subquery is multi row and multi column
problem 1: Query salary greater than 5000 The employees' , Names from which departments
decompose : Query salary greater than 5000 The employees'
SELECT dept_id FROM emp WHERE salary > 5000;
Query the Department name according to the sub query results
SELECT dept.name FROM dept WHERE dept.id IN (SELECT dept_id FROM emp WHERE salary > 5000);

边栏推荐
- 3623. Merge two ordered arrays
- Go JWT learning summary
- Wikipedia API User Guide
- Pipeline pipeline project construction
- How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic benefits of ASI, VR, arbr, DPO and trix indicators
- Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
- 五篇经典好文,值得一看(2)
- Common skills for quantitative investment - drawing 2: drawing the moving average
- 数学知识整理:极值&最值,驻点,拉格朗日乘子
- Leetcode question brushing 06 bit operation
猜你喜欢
![[Latex] 插入图片](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[Latex] 插入图片
![[projet cs144 de Stanford Computing Network] lab1: Stream reassembler](/img/7b/fad18b68a6ee30d1dec4dad6273b98.png)
[projet cs144 de Stanford Computing Network] lab1: Stream reassembler

軟件測試的幾種分類,一看就明了
![[Stanford Jiwang cs144 project] lab1: streamreassembler](/img/7b/fad18b68a6ee30d1dec4dad6273b98.png)
[Stanford Jiwang cs144 project] lab1: streamreassembler

ArrayList underlying source code

Unity calls alertdialog

How to print infinite symbol in WPS

Set and array conversion, list, array

ES6 deconstruction assignment

Leetcode question brushing 02 linked list operation
随机推荐
The tle4253gs is a monolithic integrated low dropout tracking regulator in a small pg-dso-8 package.
Quick power explanation
Mathematical knowledge arrangement: extremum & maximum, stagnation point, Lagrange multiplier
Five classic articles worth reading (2)
MySQL exception: com mysql. jdbc. PacketTooBigException: Packet for query is too large(4223215 > 4194304)
Stmarl: a spatio temporal multi agentreinforcement learning approach for cooperative traffic
Application advantages of 5g industrial gateway in coal industry
Work and life
Lessons learned from the NLP part of the course "Baidu architects hands-on deep learning"
五篇经典好文,值得一看(2)
5G工业网关在煤矿行业的应用优势
A problem discovery and attempted solution to the strange stop of server script
How the ET framework uses it to develop games
Golang context (context summary)
Leetcode-11- container with the most water (medium)
4K sea bottom and water surface fabrication method and ocean bump displacement texture Download
MySQL异常:com.mysql.jdbc.PacketTooBigException: Packet for query is too large(4223215 > 4194304)
707. design linked list
Answer to matrix theory of Nanjing University of Aeronautics and Astronautics
Golang inline mechanism & go development test