首頁  >  文章  >  資料庫  >  怎樣查詢兩個表中同一欄位的不同資料值

怎樣查詢兩個表中同一欄位的不同資料值

怪我咯
怪我咯原創
2017-06-23 13:34:129833瀏覽

怎麼查詢兩個表中同一欄位的不同資料值

例如:

A表中的欄位a有40000條資料
B表中的欄位a有60000筆數據,其中的40000條數據跟A表是一樣的
怎麼能把那不一樣的20000條數據查詢出來啊?

--建表table1,table2:   

create   table   table1(id   int,name   varchar(10));   
create   table   table2(id   int,score   int);   
insert   into   table1   select   '1','lee';
insert   into   table1   select   '2','zhang';
insert   into   table1   select   '3','steve';
insert   into   table1   select   '4','wang';   
insert   into   table2   select   '1','90';   
insert   into   table2   select   '2','100';   
insert   into   table2   select   '3','70';

#如表 

------------ --------------------------------------   
table1    
------ -------------------------------------------   
id ​​name  

1 lee

2 zhang

3 steve

4 wang

----------------------- --------------------------  

table2

----------- -------------------------------------- 

id ​​score

1 90

2 100

3 70

------------------------------------ ------------------- 

 

(1)左側向外連結的結果集包括   left   outer   子句中指定的左表格的所有行,而不僅僅是聯接列所符合的行。如果左表的某行在右表中沒有符合行,則在相關聯的結果集行中右表的所有選擇清單列均為空值(null)。

(2)sql語句  

select * from table1 t1 left join table2 t2 on t1.id = t2.id


#3 steve 3 70
-------------結果-------------   
id ​​name id score   
-------------------------- ----   

1 lee 1 90   

2 zhang 2 100  

4 wang null null   

------------------- -----------  

註解:包含table1的所有子句,根據指定條件返回table2對應的字段,不符合的以null顯示

(3)那麼取得差值1
#select from table1 t1 left join table2 t2 on t1 .id = t2.id WHERE t2.id is 
null

 

null



## 

-------------結果-------------   

id ​​name id score

4 wang null null   

------------------------------   where 
以下是工作中實際遇到的情況:
##過濾出0銷售人員(即沒有銷售記錄的員工資訊清單)。
#銷售人員(使用者角色中間表)
#1select userid from
 
bbscs_role_user 
roleid = 

##where

 

roleid = 

'sales'

'sales'

#'sales'#'sales##」 type = 4 #and##卷
 # ---> 11筆記錄 統計表(使用者銷售記錄表)1#select refid from bbscs_sales_income_stat 
where
 
= ######'2012-02'### ###and### ###amount != 0######################################################################################## ###

 

 # ---> 4筆記錄

要求為:另外7個銷售人員的記錄列出來為目的。

 

#這個是SQL語句模型 BEGINis null
##1select from b t2 left join a t1 on t1.a1 = t2.b1 WHERE
#t1.a1 

 

##這個是SQL語句模型 END (
#說明:左表是資料多的那個表(基準表如b表) 。 left join查詢。 where條件是右邊的那個表(a表)某個欄位(a1)為Null作為(判斷欄位)
 
##將SQL傳回結果當作臨時表來查詢
123select from (select userid from
bbscs_role_user ##frombbscs_role_user 
## 從## 從##好的h好的#where roleid = 'sales') t2 left 
join
select### ###refid ######from### ###bbscs_sales_income_stat ######where### ###type =####type =####type 4 ######and### ###month### ###= ######'2012-02'### ##########and### # ##amount != 0) t1 ######on### ###t2.userid = t1.refid ######WHERE### ####t1.refid #######WHERE### ####t1.refid ########WHERE### ####t1.refid ####### is### ###null######################

 

 # --->7筆記錄

 

 

測試一:

##SQL語句, mysql 查詢兩個表中不同的值(主要是差值)  這個語句查詢還是有問題。

;
1
2
#select t1.Userid from bbscs_role_user t1 ##from#joinbbscs_role_user t1 left join bbscs_sales_income_stat t2 
bbscs_sales_income_stat t2 
##on# # t1.userid = t2.refid t1.roleid = #'sales' and  t2.type = 4 and t2.month '2012-02 ' and t2.amount != 0 where t2.id 
null

# ##表格與表格,條件與條件獨立。

 # --->18筆記錄

 

 測試二:and t2.
#1
2select t1.Userid from#bbscs_role_user t1 left join bbscs_sales_income_stat t2 on #o##t1.userid and t1.roleid = 'sales' and 
t2.type = 4 
###month### ###= ######'2012-02'### ###and### #### t2.amount != 0 ######and### ###t2.id ######is### ####null######################################################################################################## #######

##where or and 區別

 # --->22筆記錄

 

 

更強大的臨時表格查詢功能,將以上查詢結果放入一個整體。

##跟用戶部門中間表關聯,依部門id排序顯示。

 WHERE 
1
#2
3
select t4.userid fromselect #*  #from (select userid from bbscs_role_user where ## roleid = 'sales') t2 left join
 
#(select refid from bbscs_sales_income_stat where type = 4 and## type = 4 
#CC month '2012-02' and amount != 0) t1 on
t2.userid = t1.refid 
t1.refid 


#is

 ###null### ###) t3, bbscs_org_user t4 ######where### ###t3.userid = t4.userid ######order### ###by### ####orgId #############################

以上是怎樣查詢兩個表中同一欄位的不同資料值的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn