1.目前有t_a、t_b、t_c三个表:
CREATE TABLE t_A ( id int(3), name VARCHAR(10) ); CREATE TABLE t_B ( id int(3), name VARCHAR(10) ); CREATE TABLE t_C ( id int(3), name VARCHAR(10) );
INSERT INTO t_A VALUES(1,'A'); INSERT INTO t_A VALUES(2,'B'); INSERT INTO t_A VALUES(3,'C'); INSERT INTO t_A VALUES(4,'D'); INSERT INTO t_A VALUES(5,'E'); INSERT INTO t_B VALUES(1,'AA'); INSERT INTO t_B VALUES(1,'BB'); INSERT INTO t_B VALUES(2,'CC'); INSERT INTO t_B VALUES(1,'DD'); INSERT INTO t_C VALUES(4,'AAC'); INSERT INTO t_C VALUES(5,'BBC'); INSERT INTO t_C VALUES(5,'CCC'); INSERT INTO t_C VALUES(3,'DDC');
2.思路:t_a为主表,t_a在t_b的左边,在t_c的右边
SELECT * FROM t_c c RIGHT JOIN t_a a ON a.id=c.id LEFT JOIN t_b b ON a.id=b.id ;
3.结果:
注意:oracle也一样 ,个人觉得oracle用(+)来写比较好看。