九、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安全

 

(運行方法爲逐條查詢)code

 

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 hahaio

 

 

 

 

鏈接兩表之間的簡便寫法:

1.select

 

select haha.name,sex,age,bumen.name,ceo from bumen,haha where haha.bumen = bumen.code數據類型

 

 

2.join on(順序可顛倒)nio

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

 

4left的用法

在沒有關係的狀況下,只顯示join左邊表的全部數據,不顯示右邊表的數據

5right的用法

同上

(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

相關文章
相關標籤/搜索