T1表 | userid (int) | username varchar(20) | password varchar(20) |
| 1 | jack | jackpwd |
| 2 | owen | owenpwd |
| |||
T2表 | userid (int) | jifen varchar(20) | dengji varchar(20) |
| 1 | 20 | 3 |
| 3 | 50 | 6v |
内联(inner join)
把两个表中都存在userid的行拼成一行
SQL语句:select * from T1 inner join T2 on T1.userid=T2.userid
运行结果 | T1.userid | username | password | T2.userid | jifen | dengji |
| 1 | jack | jackpwd | 1 | 20 | 3 |
左联(left outer join)
显示左表T1中的所有行,并把右表T2中符合条件加到左表T1中;右表T2中不符合条件,就不用加入结果表中,并且NULL表示。
SQL语句:select * from T1 left outer join T2 on T1.userid=T2.userid
运行结果 | T1.userid | username | password | T2.userid | jifen | dengji |
| 1 | jack | jackpwd | 1 | 20 | 3 |
| 2 | owen | owenpwd | NULL | NULL | NULL |
右联(right outer join)
显示右表T2中的所有行,并把左表T1中符合条件加到右表T2中;左表T1中不符合条件,就不用加入结果表中,并且NULL表示。
SQL语句:select * from T1 right outer join T2 on T1.userid=T2.userid
运行结果 | T1.userid | username | password | T2.userid | jifen | dengji |
| 1 | jack | jackpwd | 1 | 20 | 3 |
| NULL | NULL | NULL | 3 | 50 | 6 |
全联(full outer join)
显示左表T1、右表T2两边中的所有行,即把左联结果表+右联结果表组合在一起,然后过滤掉重复的。
SQL语句:select * from T1 full outer join T2 on T1.userid=T2.userid
运行结果 | T1.userid | username | password | T2.userid | jifen | dengji |
| 1 | jack | jackpwd | 1 | 20 | 3 |
| 2 | owen | owenpwd | NULL | NULL | NULL |
| NULL | NULL | NULL | 3 | 50 | 6 |