首页 > 代码库 > 数据库的左连接,右连接,内链接:
数据库的左连接,右连接,内链接:
先建立数据库,代码如下:
create database if not exists "emp";
use emp;
drop table if exists "dept";
create table dept( `depid` varchar(50) NOT NULL, `deptname` varchar(100) default NULL, PRIMARY KEY (`depid`) )
engine=innodb default charset=utf-8;
insert into dept(`depid`,`deptname`) values (‘01‘,‘部门1‘),(‘02‘,‘部门2‘),(‘03‘,‘部门3‘),(‘04‘,‘部门4‘);
drop table if exists "emp";
create table emp (`empid` varchar(50) NOT NULL, `empname` varchar(50) default NULL, `depid` varchar(50) default NULL,PRIMARY KEY (`empid`) ) engine=innodb default charset=utf-8;
insert into emp(`empid`,`empname`,`depid`) values (‘0001‘,‘柳梦璃‘,‘01‘),(‘0002‘,‘韩菱纱‘,‘01‘),(‘0003‘,‘云天河‘,‘02‘),(‘0004‘,‘慕容紫英‘,‘03‘),(‘0005‘,‘玄霄‘,NULL),(‘0006‘,‘九天玄女‘,NULL);
左连接:select * from dept d left join emp e where d.depid=e.empid;
右连接:select * from dept d right join emp e where d.depid=e.empid;
内链接:select * from dept d innner/union join emp e where d.depid=e.empid;
(内链接可以省略inner/union关键字)
数据库的左连接,右连接,内链接: