发布网友 发布时间:2022-04-30 05:32
共2个回答
懂视网 时间:2022-04-30 09:53
例子:
表TESTA,TESTC,TESTD各有A, B两列
TESTB有A,C两列
一、自然连接
SELECT * FROM TESTA NATURAL JOIN TESTB;
二、内连接:这边我们演示的是等值连接
方式一:SELECT * FROM TESTA a ,TESTC c WHERE a.A=c.A;
方式二:SELECT * FROM TESTA a INNER JOIN TESTC c ON (a.A=c.A);
结果表如下:
三、外连接:
1.左外连接 left outer join 或者 left join
左外连接就是在等值连接的基础上加上主表中的未匹配数据,例:
写法一:SELECT * FROM TESTA a LEFT OUTER JOIN TESTC c ON (a.A=c.A);
写法二:SELECT * FROM TESTA a LEFT JOIN TESTC c ON (a.A=c.A);
ORACLE特有写法:SELECT * FROM TESTA a,TESTC c WHERE a.A=c.A(+);
结果表如下:
2.右外连接 right outer join 或者 right join
右外连接是在等值连接的基础上加上被连接表的不匹配数据
写法一:SELECT * FROM TESTA a RIGHT OUTER JOIN TESTC c ON (a.A=c.A);
写法二:SELECT * FROM TESTA a RIGHT JOIN TESTC c ON (a.A=c.A);
ORACLE特有写法:SELECT * FROM TESTA a,TESTC c WHERE a.A(+)=c.A;
结果表如下:
3.全外连接 full outer join 或者 full join
全外连接是在等值连接的基础上将左表和右表的未匹配数据都加上
写法一:SELECT * FROM TESTA a FUll OUTER JOIN TESTC c ON (a.A=c.A);
写法二:SELECT * FROM TESTA a FUll JOIN TESTC c ON (a.A=c.A);
等价写法:
SELECT * FROM TESTA a LEFT JOIN TESTC c ON (a.A=c.A)
UNION
SELECT * FROM TESTA a RIGHT JOIN TESTC c ON (a.A=c.A);
结果表如下:
数据库连接之内连接、自然连接、外连接区分
标签:
热心网友 时间:2022-04-30 07:01
关键字: 左右连接