-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjoin_output.sql
107 lines (98 loc) · 2.1 KB
/
join_output.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
drop table if exists t1, t2, t3;
create table if not exists t1
(
c1 int primary key,
c2 int
);
create table if not exists t2
(
c1 int primary key,
c2 int
);
create table if not exists t3
(
c1 int primary key,
c2 int
);
insert into t1(c1, c2)
values (1, 1),
(2, 2),
(3, 3),
(4, 4);
select *
from t1;
insert into t2(c1, c2)
values (1, 1),
(4, 4),
(5, 5),
(6, 6);;
insert into t3
select *
from t1;
## inner join 同full join 带on
select *
from t1
inner join t2 on t1.c1 = t2.c1;
## join 同inner join
select *
from t1
join t2 on t1.c1 = t2.c1;
## cross join 即inner join
select *
from t1
cross join t2 on t1.c1 = t2.c1;
# +----+------+----+------+
# | c1 | c2 | c1 | c2 |
# +----+------+----+------+
# | 1 | 1 | 1 | 1 |
# | 4 | 4 | 4 | 4 |
# +----+------+----+------+
## left join
select *
from t1
left join t2 on t1.c1 = t2.c1;
# +----+------+------+------+
# | c1 | c2 | c1 | c2 |
# +----+------+------+------+
# | 1 | 1 | 1 | 1 |
# | 2 | 2 | NULL | NULL |
# | 3 | 3 | NULL | NULL |
# | 4 | 4 | 4 | 4 |
# +----+------+------+------+
## right join
select *
from t1
right join t2 on t1.c1 = t2.c1;
# +------+------+----+------+
# | c1 | c2 | c1 | c2 |
# +------+------+----+------+
# | 1 | 1 | 1 | 1 |
# | 4 | 4 | 4 | 4 |
# | NULL | NULL | 5 | 5 |
# | NULL | NULL | 6 | 6 |
# +------+------+----+------+
## full join , 笛卡尔积 , 4 * 4 ;
select *
from t1 full
join t2;
# +----+------+----+------+
# | c1 | c2 | c1 | c2 |
# +----+------+----+------+
# | 1 | 1 | 1 | 1 |
# | 2 | 2 | 1 | 1 |
# | 3 | 3 | 1 | 1 |
# | 4 | 4 | 1 | 1 |
# | 1 | 1 | 4 | 4 |
# | 2 | 2 | 4 | 4 |
# | 3 | 3 | 4 | 4 |
# | 4 | 4 | 4 | 4 |
# | 1 | 1 | 5 | 5 |
# | 2 | 2 | 5 | 5 |
# | 3 | 3 | 5 | 5 |
# | 4 | 4 | 5 | 5 |
# | 1 | 1 | 6 | 6 |
# | 2 | 2 | 6 | 6 |
# | 3 | 3 | 6 | 6 |
# | 4 | 4 | 6 | 6 |
# +----+------+----+------+