@ Toc
more knowledge about the database, please add attention yo ~ ~. For bloggers please contact Gabor main private letter or contact:
QQ: 3327908431
micro letter: ZDSL1542334210
Introduction: MySQL series of operations fierce as a tiger, the code clear and easy to understand, simple structure, today let's talk about MySQL powerful multi-table joins, after the connection table, not only to facilitate inquiries, processing, and easier to do data analysis, as we save a lot of trouble, ado, today to explain directly into the picture. **
1, multi-table connection types and grammar
1.1, the longitudinal splice
Union also called splice longitudinal splice, two matching table rows based on the value of the total column of each table, to be connected to two table must have the same column name can, if two different or not all the same order Field , then be replaced by a query. The syntax is:
select * from t1 union select * from t2; # 两表列名和列顺序完全相同
select 列名称 from t1 union select 列名称 from t2;
1.2, the outer coupling
This kind of connection is the most common and more difficult to understand a connection. Coupling may be left outside the outer join, the right outer coupling or full outer join. When specified in the FROM clause outer join, it may consist of the following groups of keywords in a group designated:
. 1) the Join left or left coupling left outer join result set includes all of the left outer clause specified in the left table line, not just the matching join column row. If you do not match the right table rows in a row left the table, the rows in the result set associated with the right table of all the select list columns are null values, but need to specify the primary key id, the name of the two main keys to the same table the syntax is:
select * from t1 left join t2 on t1.id=t2.id;
2) right join or right outer join right outer join is the reverse link of the left outer join. Returns all rows in the right table. If there are no matching rows in a row left the table and right table will return a null value for the left table.
select * from t1 right join t2 on t1.id=t2.id;
1.3, inner join
inner join two tables show only the data associated is that when two identical primary key table does not display the results. The following example, when the scores inside s_id incomplete, then the connection will not s_id scores corresponding row is not displayed.
select * from stu inner join scores on stu.s_id = scores.s_id;
1.4, the cross coupling (Cartesian product)
In cross-coupling join = full join returns all rows are left in the table, all combinations of each row of the left row of the right table in the table, do not need to specify the primary key. Cross coupled also called Cartesian products. Clause can be specified from the table or view by coupling the external coupling or complete in any order; however, when coupled with a left or right outer specified table or view, the sequence table or view is very important.
select * from t1 join t2; # 法一
select * from t1 full join t2;# 法二
2, table creation
Creating student information table stu
create table stu(s_id varchar(5),
s_name varchar(5),
s_sex varchar(1),
s_age int(3),s_day date);
insert into stu values
("001","李华","男",23,'1996-8-16'),
("002","王二","男",24,'1997-3-16'),
("003","赵敏","女",23,'1990-5-26'),
("004","张莹莹","女",22,'1995-2-16'),
("005","朱亚军","男",25,'1999-8-16'),
("006","马云","男",28,'1993-12-16');
Create scores of students scoring table
create table scores (s_id varchar(5),
c_id varchar(3),score float);
insert into scores values
("001","01",135),
("005","01",120),
("003","01",110),
("002","01",90),
("005","02",140),
("001","02",125.5),
("004","02",100),
("006","02",90),
("002","03",102),
("005","03",100.6),
("001","03",100),
("003","03",95.6),
("004","03",83),
("003","02",80),
("006","03",79.5);
Creating a curriculum coure
create table coure(
c_id varchar(2),t_id char(1),c_name varchar(10));
insert into coure values
("01","3","数据库原理"),
("02","2","统计学基础"),
("03","1","Python基础");
Create table teacher teacher information
create table teacher(
t_id char(1),t_name varchar(5));
insert into teacher values
("1","邓博"),
("2","崔博"),
("3","汪院长");
Create table t1
create table t1(s_id varchar(5),
s_name varchar(5),
s_sex varchar(1),
s_age int(3),s_day date);
insert into stu values
("007","走走走","男",105,'1996-8-16');
3, section title
Topic one: stu table and left scores table connection
select * from stu left join scores on stu.s_id=scores.s_id;
# 答案:
001 李华 男 23 1996-8-16 001 01 135
005 朱亚军 男 25 1999-8-16 005 01 120
003 赵敏 女 23 1990-5-26 003 01 110
002 王二 男 24 1997-3-16 002 01 90
005 朱亚军 男 25 1999-8-16 005 02 140
001 李华 男 23 1996-8-16 001 02 125.5
004 张莹莹 女 22 1995-2-16 004 02 100
006 马云 男 28 1993-12-16 006 02 90
002 王二 男 24 1997-3-16 002 03 102
005 朱亚军 男 25 1999-8-16 005 03 100.6
001 李华 男 23 1996-8-16 001 03 100
003 赵敏 女 23 1990-5-26 003 03 95.6
004 张莹莹 女 22 1995-2-16 004 03 83
003 赵敏 女 23 1990-5-26 003 02 80
006 马云 男 28 1993-12-16 006 03 79.5
Topic Two: stu tables and scores table and the table do coure connection
analysis: multi-table joins nothing more than more than a few tables, the specific syntax is:
select * from t1 left join t2 on ti.id=t2.id
left join t3 on t2.id=t3.id left join t4 on t3.id=t4.id ... ;
Here we must ensure that information is not lost, and where different column names, using left join, according to the characteristics of the known data, are:
select * from stu left join scores on stu.s_id=scores.s_id
left join coure on scores.c_id=coure.c_id;
# 答案:
001 李华 男 23 1996-8-16 001 01 135 01 3 数据库原理
005 朱亚军 男 25 1999-8-16 005 01 120 01 3 数据库原理
003 赵敏 女 23 1990-5-26 003 01 110 01 3 数据库原理
002 王二 男 24 1997-3-16 002 01 90 01 3 数据库原理
005 朱亚军 男 25 1999-8-16 005 02 140 02 2 统计学基础
001 李华 男 23 1996-8-16 001 02 125.5 02 2 统计学基础
004 张莹莹 女 22 1995-2-16 004 02 100 02 2 统计学基础
006 马云 男 28 1993-12-16 006 02 90 02 2 统计学基础
003 赵敏 女 23 1990-5-26 003 02 80 02 2 统计学基础
002 王二 男 24 1997-3-16 002 03 102 03 1 Python基础
005 朱亚军 男 25 1999-8-16 005 03 100.6 03 1 Python基础
001 李华 男 23 1996-8-16 001 03 100 03 1 Python基础
003 赵敏 女 23 1990-5-26 003 03 95.6 03 1 Python基础
004 张莹莹 女 22 1995-2-16 004 03 83 03 1 Python基础
006 马云 男 28 1993-12-16 006 03 79.5 03 1 Python基础
007 走走走 男 105 1996-8-16
Topic three: the stu, scores, coure and teacher table to do to connect
select * from stu left join scores on stu.s_id=scores.s_id
left join coure on scores.c_id=coure.c_id left join teacher on coure.t_id=teacher.t_id;
Title IV: Table stu longitudinal connection table and t1;
select * from stu union select * from t1;
# 答案
001 李华 男 23 1996-8-16
002 王二 男 24 1997-3-16
003 赵敏 女 23 1990-5-26
004 张莹莹 女 22 1995-2-16
005 朱亚军 男 25 1999-8-16
006 马云 男 28 1993-12-16
007 走走走 男 105 1996-8-16
Topic Five: The longitudinal connecting teacher coure table and table;
Analysis: As the table only two teachers in the same id this column, but this will result duplicated, so do not use union all repeat, so to achieve vertical connection is only so
select t_id from teacher union all select t_id from coure;
# 答案
1
2
3
3
2
1
4, the end of the paper eggs - easy moment
Well we all know, men to 17 and 18 years old it was somewhat what, you say my good friend Winnie appeared over this thing, before high school Well, we will talk about a girlfriend squad, but he also shared two , and then after a few weeks we eat together, since they lost not seen for several weeks, the results bear ridicule: "! you have to die sooner or later the kid in the woman," and then leave the squad mouth said: "you'll end up dead themselves right hand! "and then bear silently bowed his head, looked at his right hand, did a lot of thick calluses than before ... Well is such a case.
Today to end here yo // each article has the end the egg - relaxed moment yo ~ plus interest to learn more about MySQL knowledge! Thank you for watching, I was Jetuser-data
Links: [https://blog.csdn.net/L1542334210]
CSND: L1542334210
I wish you all success! Family fun!