鸣谢: https://blog.csdn.net/plg17/article/details/78758593
CREATE TABLE `a_table` (
`a_id` int(11) PRIMARY KEY,
`a_name` varchar(10) DEFAULT NULL,
`a_part` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `b_table` (
`b_id` int(11) DEFAULT NULL,
`b_name` varchar(10) DEFAULT NULL,
`b_part` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into b_table value(2,"老王","秘书部");
insert into b_table value(3,"老张","设计部");
insert into b_table value(5,"老刘","人事部");
insert into b_table value(6,"老黄","生产部");
insert into a_table value(1,"老潘","总裁部");
insert into a_table value(2,"老王","秘书部");
insert into a_table value(3,"老张","设计部");
insert into a_table value(4,"老李","运营部");
内连接:
select * from a_table a inner join b_table b on a.a_id = b.b_id;
外连接:
左连接:
select * from a_table a left join b_table bon a.a_id = b.b_id;
全连接:mysql没有提供全连接,只能用left join union select right join才行
SELECT * FROM t_blog LEFT JOIN t_type ON t_blog.typeId=t_type.id
UNION
SELECT * FROM t_blog RIGHT JOIN t_type ON t_blog.typeId=t_type.id;