/*左表*/
CREATE TABLE L AS
SELECT 'left_1' AS str,'1' AS v FROM dual UNION ALL
SELECT 'left_2','2' AS v FROM dual UNION ALL
SELECT 'left_3','3' AS v FROM dual UNION ALL
SELECT 'left_4','4' AS v FROM dual;
/*右表*/
CREATE TABLE R AS
SELECT 'right_3' AS str,'3' AS v,1 AS status FROM dual UNION ALL
SELECT 'right_4' AS str,'4' AS v,0 AS status FROM dual UNION ALL
SELECT 'right_5' AS str,'5' AS v,0 AS status FROM dual UNION ALL
SELECT 'right_6' AS str,'6' AS v,0 AS status FROM dual;
SQL> select * from l;
STR V
------ -
left_1 1
left_2 2
left_3 3
left_4 4
SQL> select * from r;
STR V STATUS
------- - ----------
right_3 3 1
right_4 4 0
right_5 5 0
right_6 6 0
----左连接
SQL> SELECT L.str AS LEFT_str,R.str AS RIGHT_str FROM L LEFT JOIN R ON L.v = R.v ORDER BY 1,2;
LEFT_S RIGHT_S
------ -------
left_1
left_2
left_3 right_3
left_4 right_4
SQL> SELECT L.str AS LEFT_str,R.str AS RIGHT_str FROM L,R WHERE L.v = R.v(+) ORDER BY 1,2;
LEFT_S RIGHT_S
------ -------
left_1
left_2
left_3 right_3
left_4 right_4
R.v(+) ,没有匹配的行用空行补齐。
----右连接
SQL> SELECT L.str AS LEFT_str,R.str AS RIGHT_str FROM L RIGHT JOIN R ON L.v = R.v ORDER BY 1,2;
LEFT_S RIGHT_S
------ -------
left_3 right_3
left_4 right_4
right_5
right_6
SQL> SELECT L.str AS LEFT_str,R.str AS RIGHT_str FROM L,R WHERE L.v(+) = R.v ORDER BY 1,2;
LEFT_S RIGHT_S
------ -------
left_3 right_3
left_4 right_4
right_5
right_6
---全连接
SQL> SELECT L.str AS LEFT_str,R.str AS RIGHT_str FROM L FULL JOIN R ON R.v = L.v ORDER BY 1,2;
LEFT_S RIGHT_S
------ -------
left_1
left_2
left_3 right_3
left_4 right_4
right_5
right_6
已选择6行。