当前位置:网站首页>MySQL advanced query
MySQL advanced query
2022-06-13 05:15:00 【strive_ one】
Join query operation
- When the columns of query results are from multiple tables , You need to join multiple tables into a large dataset , Select the appropriate column to return to .
mysql Three types of connection queries are supported , Namely :
- Internal connection query :
- The query result is the data that the two tables match
- Right connection query :
- The result of the query is the data matched by the two tables , Data unique to the right table , For data that does not exist in the left table, use null fill
- Left connection query :
- The result of the query is the data matched by the two tables , Data unique to the left table , Use the data that does not exist in the right table of teammates null fill
example :
- Now create a class table , A student watch :
create table classes(
id int unsigned primary key auto_increment not null,
name varchar(20) default '',
isdelete bit default 0
);
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default '',
age tinyint unsigned default 0,
height decimal(5,2), -- Maximum 5 Digit number , Two decimal places
gender enum(' male ',' Woman ',' Simon? ',' A secret '),
cls_id int unsigned default 0,
isdelete bit default 0
);
- Insert data for the table
insert into classes values
(1,"python1", 0),
(2,"python2",0),
(3,"python3",0),
(4,"python4",0),
(6,"python5", 1);
insert into students values
(0,' Wei Shao ',18,180.00,2,1,0),
(0,' Little moon ',18,180.00,2,2,1),
(0,' peng ',29,185.00,1,1,0),
(0,' Lau Andy ',59,175.00,1,2,1),
(0,' lotus ',38,160.00,2,1,0),
(0,' feng ',28,150.00,4,2,1),
(0,' Joey wong ',18,172.00,2,1,1),
(0,' Jay Chou ',36,NULL,1,1,0),
(0,' Cheng Kun ',27,181.00,1,2,0),
(0,' Liu Yifei ',25,166.00,2,2,0),
(0,' Venus ',33,162.00,3,3,1),
(0,' Jingxiang ',12,180.00,2,4,0),
(0,' Zhou Jie ',34,176.00,2,5,0);
Link query
Internal connection query :inner join … on …
- on Followed by the conditions of the connection query .
- Use the inner link to query the class table and the student table
- Link query
- Query the class name and the class name of the student
- Student name : stay students surface
- Class name : stay classes surface
select students.name,classes.name from students inner join classes on students.cls_id = classes.id;
Left connection query :left join on
- Left join queries have master tables and slave tables
- left The previous main table ,left The following is the slave table
- All the data in the main table will be displayed ( Whether the connection condition is satisfied or not, it will display )
select s.name,c.name from students as s left join classes as c on s.cls_id = c.id;
Right link query :right join on
- Right connection query :right The preceding is the slave table ,right The following is the main table
select s.name,c.name from students as s right join classes as c on s.cls_id = c.id;
Extended learning : Show all the students in the class
- The first method of inner connection :
select c.name,group_concat(s.name) from students as s,classes as c where s.cls_id = c.id group by c.name;
- The second method of inner connection :
select c.name,group_concat(s.name) from students as s inner join classes as c on s.cls_id = c.id group by c.name;
- Practice of left connection :
select c.name,group_concat(s.name) from students as s left join classes as c on s.cls_id = c.id group by c.name;
- The practice of right connection :
select c.name,group_concat(s.name) from students as s right join classes as c on s.cls_id = c.id group by c.name;
Two shorthand modes of inner connection
- join on
select s.name, c.name from students as s join classes as c on s.cls_id = c.id;
- The second shorthand :cross join on
select s.name, c.name from students as s cross join classes as c on s.cls_id = c.id;
边栏推荐
- Chapter 15 mechanism: Address Translation
- Chapter 13 abstraction: address space
- Section 7 - structures
- Shell instance
- ZABBIX wechat alarm
- Regular expressions in QT
- Flex布局自适应失效的问题
- Clause 27: alternatives to overloading with familiar universal reference types
- Dup2 use
- The problem of flex layout adaptive failure
猜你喜欢
Elliptic curve encryption
Spread your wings and soar
ZABBIX proxy, sender (without agent monitoring), performance optimization
Float type value range
QT brushes and brushes
Chapter 2 process management
QT realizes message sending and file transmission between client and server
Pycharm错误解决:Process finished with exit code -1073741819 (0xC0000005)
Case - simulated landlords (upgraded version)
Simple SR: best buddy Gans for highly detailed image super resolution
随机推荐
QT using layout manager is invalid or abnormal
MySQL installation, architecture and management
Reductive elimination
Robot pose description and coordinate transformation
Interpretation of QT keypressevent
The problem of flex layout adaptive failure
安装harbor(在线|离线)
C language learning log 1.17
ZABBIX wechat alarm
【多线程】线程池核心类-ThreadPoolExecutor
Wang Dao Chapter II linear table exercises
Case - traversing the directory (file class & recursive call)
File descriptorfile description
Clause 48: understand template metaprogramming
Dynamic and static libraries
float类型取值范围
List collection concurrent modification exception
[thread / multithread] execution sequence of threads
Shell variable learning notes
Listiterator list iterator