博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySql常用 join 详解
阅读量:5231 次
发布时间:2019-06-14

本文共 2625 字,大约阅读时间需要 8 分钟。

虽然这类资料比较多....我觉得还是有必要记下来,新手可以看看吧。。。老司机可以一眼飘过那。。。

常用SQL JOINS方式

  • 1.SELECT select_list FROM TABLEA A LEFT JOIN TABLEB B ON A.Key=B.Key

  • 2.SELECT select_list FROM TABLEA A RIGHT JOIN TABLEB B ON A.Key=B.Key

  • 3.SELECT select_list FROM TABLEA A LEFT JOIN TABLEB B ON A.Key=B.Key

    WHERE B.key is NULL

  • 4.SELECT select_list FROM TABLEA A RIGHT JOIN TABLEB B ON A.Key=B.key

    WHERE A.Key is null

  • 5.SELECT select_list FROM TABLE A INNER JOIN TABLEB B ON A.Key=B.Key

  • 6.SELECT select_list FROM TABLE A FULL OUTER JOIN TABLEB B ON A.Key=B.Key(Oracle支持)

  • 7.SELECT select_list FROM TABLE A FULL OUTER JOIN TABLEB B ON

    WHERE A.Key IS NULL OR B.Key IS NULL(Oracle支持)

运行效果
create table tbl_dept(  id int(11) not null auto_increment,  deptName varchar(30) default null,  locAdd varchar(40) default null,  primary key(id))engine=innodb auto_increment=1 default charset=utf8;create table tbl_emp(  id int(11) not null auto_increment,  name varchar(20) default null,  deptId int(11) default null,  primary key(id),  key fk_dept_id (deptId)  #constraint fk_dept_id foreign key (deptId) references tbl_dept (id))engine=innodb auto_increment=1 default charset=utf8;insert into tbl_dept(deptName,locAdd)values('RD',11);insert into tbl_dept(deptName,locAdd)values('HR',12);insert into tbl_dept(deptName,locAdd)values('MK',13);insert into tbl_dept(deptName,locAdd)values('MIS',14);insert into tbl_dept(deptName,locAdd)values('FD',16);insert into tbl_emp(name,deptId)values('z2',1);insert into tbl_emp(name,deptId)values('z3',1);insert into tbl_emp(name,deptId)values('z4',1);insert into tbl_emp(name,deptId)values('z4',1);insert into tbl_emp(name,deptId)values('z6',1);insert into tbl_emp(name,deptId)values('w5',2);insert into tbl_emp(name,deptId)values('s7',3);insert into tbl_emp(name,deptId)values('s8',4);insert into tbl_emp(name,deptId)values('s9',51)

  • select * from tbl_emp;

  • select * from tbl_emp;

  • 两个集合笛卡尓积 select * from tbl_emp,tbl_dept;

  • select * from tbl_emp a inner join tbl_dept b on a.deptId=b.id;

  • select * from tbl_emp a left join tbl_dept b on a.deptId=b.id;

  • select * from tbl_emp a right join tbl_dept b on a.deptId=b.id;

  • select * from tbl_emp a left join tbl_dept b on a.deptId=b.id where b.id is null;

  • select * from tbl_emp a right join tbl_dept b on a.deptId=b.id where a.deptId is null;

  • select * from tbl_emp a left join tbl_dept b on a.deptId=b.id

    union
    select * from tbl_emp a right join tbl_dept b on a.deptId=b.id;

  • select * from tbl_emp a left join tbl_dept b on a.deptId=b.id where b.id is null

    union
    select * from tbl_emp a right join tbl_dept b on a.deptId=b.id where a.deptId is null;

 

转载于:https://www.cnblogs.com/phpper/p/7338783.html

你可能感兴趣的文章
this 指向
查看>>
Kruskal基础最小生成树
查看>>
BZOJ.4819.[SDOI2017]新生舞会(01分数规划 费用流SPFA)
查看>>
ubuntu 14.04 安装搜狗拼音输入法
查看>>
浅谈算法和数据结构: 一 栈和队列
查看>>
[WebMatrix] 如何将SQL Compact 4.0 移转至SQL Server 2008 Express
查看>>
Java内部类详解
查看>>
python-基础
查看>>
17 案例
查看>>
【BZOJ 1221】 [HNOI2001] 软件开发
查看>>
【hdu 1429】胜利大逃亡(续)
查看>>
SQL字符型转日期型
查看>>
Java程序设计教程(第2版)阅读总结
查看>>
图论-次短路求法
查看>>
ios下opencv编译merge函数报错问题
查看>>
What's New for Visual C# 6.0
查看>>
ExtJs学习笔记之ComboBox组件
查看>>
关于收费软件
查看>>
getopt_long
查看>>
HTML5中File
查看>>