当前位置:网站首页>SQL basic syntax II
SQL basic syntax II
2022-06-12 09:09:00 【Eden Garden】
1、 Multi-table query - Internal connection query
- Inner join queries are part of the data where two tables intersect ( Data associated with primary and foreign keys )
-- Explicit inner connection
SELECT Name FROM Table name 1 [INNER] JOIN Table name 2 ON Conditions ;
-- example :
-- Query user name , Age and order number
SELECT
u.`name`, -- full name
u.`age`, -- Age
o.`number` -- The order no.
FROM
USER u -- User table
JOIN
order o -- The order sheet
ON
u.`id` = o.`uid`;
-- Implicit inner join
SELECT Name FROM Table name 1, Table name 2 WHERE Conditions ;
-- Query user name , Age . And order number
SELECT
u.`name`, -- full name
u.`age`, -- Age
o.`number` -- The order no.
FROM
USER u, -- User table
order o -- The order sheet
WHERE
u.`id`=o.`uid`;
2、 Multi-table query - External connection query
- The left outer join
- Query principle
- Query all the data in the left table , And the left and right tables have the intersection part of the data
- Query principle
-- Standard grammar OUTER It can be omitted
SELECT Name FROM Table name 1 LEFT [OUTER] JOIN Table name 2 ON Conditions ;
-- Query all user information , And the corresponding order information of the user
SELECT
u.`name`, -- full name
u.`age`, -- Age
o.`number` -- The order no.
FROM
USER u -- User table
LEFT JOIN
order o -- The order sheet
ON
u.`id`=o.`uid`;
- Right connection
- Query principle
- Query all the data in the right table , And the left and right tables have the intersection part of the data
- Query principle
-- Basic grammar OUTER It can be omitted
SELECT Name FROM Table name 1 RIGHT [OUTER] JOIN Table name 2 ON Conditions ;
-- Query all order information , And the user information of the order
SELECT
u.`name`, -- full name
u.`age`, -- Age
o.`number` -- The order no.
FROM
USER u -- User table
RIGHT JOIN
order o -- The order sheet
ON
u.`id`=o.`uid`;
3、 Multi-table query - Subquery
- Subquery Introduction
- The query statement is nested in the query statement . We call nested queries subqueries !
- Subquery - The result is a single row, single column
- As a condition , Use operators to judge !
-- Standard grammar
SELECT Name FROM Table name WHERE Name =(SELECT Aggregate functions ( Name ) FROM Table name [WHERE Conditions ]);
-- Query the name of the oldest user
SELECT NAME,age FROM USER WHERE age = (SELECT MAX(age) FROM USER);
Subquery - The result is multi row, single column
- As a condition , Use the operator in or not in Judge !
-- Standard grammar
SELECT Name FROM Table name WHERE Name [NOT] IN (SELECT Name FROM Table name [WHERE Conditions ]);
-- Check the order information of Zhang San and Li Si
SELECT number,uid FROM order
WHERE uid IN (SELECT id FROM USER WHERE NAME=' Zhang San ' OR NAME=' Li Si ');
Subquery - The result is multi row, multi column
- It can be used as a virtual table to participate in the query !
-- Standard grammar
SELECT Name FROM Table name [ Alias ],(SELECT Name FROM Table name [WHERE Conditions ]) [ Alias ] [WHERE Conditions ];
-- Query the order table id Greater than 4 Order information and user information of
SELECT * FROM USER u,(SELECT * FROM order WHERE id>4) o WHERE u.id=o.uid;
4、 Multi-table query - Self association query
- Introduction to self association query
- There are data associations in the same table . You can query the same table multiple times !
-- Create an employee table
CREATE TABLE employee(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20),
mgr INT,
salary DOUBLE
);
-- Add data
INSERT INTO employee VALUES (1001,' The Monkey King ',1005,9000.00),
(1002,' Pig eight quit ',1005,8000.00),
(1003,' The sand monk ',1005,8500.00),
(1004,' Small white dragon ',1005,7900.00),
(1005,' Tang's monk ',NULL,15000.00),
(1006,' Wusong ',1009,7600.00),
(1007,' Li Kui ',1009,7400.00),
(1008,' Lin Chong ',1009,8100.00),
(1009,' Song Jiang ',NULL,16000.00);
-- Check the names of all employees and their immediate superiors , Employees without superiors also need to inquire
/* analysis : Employee name employee surface The name of the immediate superior employee surface Conditions :employee.mgr = employee.id Query all the data in the left table , And the left and right tables intersect some data , Use left outer connection */
SELECT
t1.name, -- Employee name
t1.mgr, -- Superior number
t2.id, -- Employee number
t2.name -- Employee name
FROM
employee t1 -- The employee table
LEFT OUTER JOIN
employee t2 -- The employee table
ON
t1.mgr = t2.id;
边栏推荐
- (15) Tweenrunner
- (十二)交互组件Selectable
- On absolute value function in C language
- 分库分表会带来读扩散问题?怎么解决?
- 2022 safety officer-c certificate special operation certificate examination question bank and simulation examination
- MySQL - Import / export operation
- 【无标题】Task3 多路召回
- Chapter 3 registers (memory access)
- 【字符集八】char8_t、char16_t、char32_t、wchar、char
- (JS) three digits are separated by commas, and two decimal places are reserved (or rounded)
猜你喜欢
![Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock](/img/42/000a3e601ba1771a1ee07fcd800307.jpg)
Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock

Tool classes for extracting zip files

Building a cluster: and replacing with error
![[character set 8] char8_ t、char16_ t、char32_ t、wchar、char](/img/ef/e57fb345b36c84d3585fca0568b0ee.png)
[character set 8] char8_ t、char16_ t、char32_ t、wchar、char

Detailed explanation of iSCSI (V) -- actual operation of iSCSI client configuration

MySQL learning record - II. MySQL create table command

【字符集七】汉字的宽字符码和多字节码分别是多少

Notes on data mining in Tsinghua University (1)

Definition of polar angle and its code implementation

域名映射到指定IP
随机推荐
++ problems in C language
Does database and table splitting cause reading diffusion problems? How to solve it?
软件测试需求分析方法有哪些,一起来看看吧
Consumer configuration related
Building a cluster: and replacing with error
Binlog in mysql:
(15) Tweenrunner
Make a simple page with the websql database of HTML5:
【字符集六】宽字符串和多字节字符互转
解决当打开Unity时 提示项目已经打开,而自己之前并没有打开过(可能之前异常关闭)的问题
(13) Text rendering text
更改tabledata列名
[character set 9] will GBK be garbled when copied to unicode?
Counting haybales (difference + discretization)
抓取屏幕与毛玻璃效果
2024. 考试的最大困扰度-滑动窗口
Solution of hmaster process flash back after starting
CodeCraft-22 and Codeforces Round #795 (Div. 2) 题解
Subtractive integer (number theory)
EIP-1559