首页 > 代码库 > 9、SQL基础整理(两表连接exists,join on,union)
9、SQL基础整理(两表连接exists,join on,union)
exists的用法
select *from haha where exists (select *from bumen where bumen.code = haha.bumen and bumen.name = ‘销售部‘ )and age>35
(运行方法为逐条查询)
select name,sex,age,(select name from bumen where bumen.code = haha.bumen)as 部门 from haha
select name,sex,age,(select name from bumen where bumen.code = haha.bumen)as 部门,(select ceo from bumen where bumen.code = haha.bumen)as ceo from haha
连接两表之间的简便写法:
1.
select haha.name,sex,age,bumen.name,ceo from bumen,haha where haha.bumen = bumen.code
2.join on(顺序可颠倒)
select haha.name,sex,age,bumen.name,ceo from haha
join bumen on haha.bumen = bumen.code
3.full的用法
insert into haha values(15,‘实物‘,‘nv‘,34,5)
insert into bumen values(6,‘保安部‘,‘保证安全‘,null,null)
select bumen.name,zhineng,ceo,haha.name,sex,age from haha
full join bumen on bumen.code = haha.bumen
4.left的用法
在没有关系的情况下,只显示join左边表的所有数据,不显示右边表的数据
5.right的用法
同上
(left 和right都是与所全部显示的数据的排序方式一致)
union的用法
将两列连接起来,必须满足数据类型对应,具有自动去重的功能(按照拼音或者数字排列,打乱原有的顺序)
select*from haha where code > 10
union
select*from haha where code < 5
select name,bumen from haha where code > 10
union
select ceo,code from bumen
9、SQL基础整理(两表连接exists,join on,union)