当前位置:网站首页>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;
原网站

版权声明
本文为[strive_ one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280514548819.html