怎樣查詢兩個表中同一字段的不同數據值
例如:
A表中的字段a有40000條數據
B表中的字段a有60000條數據,其中的40000條數據跟A表是一樣的
怎樣能把那不一樣的20000條數據查詢出來啊?
--建表table1,table2:
1
2
3
4
5
6
7
8
9
|
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語句
1
|
select
*
from
table1 t1
left
join
table2 t2
on
t1.id = t2.id
|
-------------結果-------------
id name id score
------------------------------
1 lee 1 90
2 zhang 2 100
3 steve 3 70
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
|
-------------結果-------------
id name id score
4 wang null null
------------------------------
下面是工作中實際遇到的情況:
##過濾出0銷售人員(即沒有銷售記錄的員工信息列表)。
#銷售人員(用戶角色中間表)
1
|
select
userid
from
bbscs_role_user
where
roleid =
'sales'
|
# ---> 11條記錄
#統計表(用戶銷售記錄表)
1
|
select
refid
from
bbscs_sales_income_stat
where
type = 4
and
month
=
'2012-02'
and
amount != 0
|
# ---> 4條記錄
要求為:另外7個銷售人員的記錄列出來為目的。
##########這個是SQL語句模型 BEGIN##########
1
|
select
*
from
b t2
left
join
a t1
on
t1.a1 = t2.b1
WHERE
t1.a1
is
null
|
#########這個是SQL語句模型 END############
說明:左表是數據多的那個表(基准表如b表)。left join查詢。where條件是右邊的那個表(a表)某個字段(a1)為Null作為(判斷字段)
##將SQL返回結果作為臨時表來查詢
1
2
3
|
select
*
from
(
select
userid
from
bbscs_role_user
where
roleid =
'sales'
) t2
left
join
(
select
refid
from
bbscs_sales_income_stat
where
type = 4
and
month
=
'2012-02'
and
amount != 0) t1
on
t2.userid = t1.refid
WHERE
t1.refid
is
null
|
# --->7條記錄
測試一:
##SQL語句,mysql 查詢兩個表中不同的值(主要是差值) 這個語句查詢還是存在問題。
1
2
|
select
t1.Userid
from
bbscs_role_user t1
left
join
bbscs_sales_income_stat t2
on
t1.userid = t2.refid
and
t1.roleid =
'sales'
and
t2.type = 4
and
t2.
month
=
'2012-02'
and
t2.amount != 0
where
t2.id
is
null
;
|
##表與表,條件與條件獨立出來。
# --->18條記錄
測試二:
1
2
|
select
t1.Userid
from
bbscs_role_user t1
left
join
bbscs_sales_income_stat t2
on
t1.userid = t2.refid
and
t1.roleid =
'sales'
and
t2.type = 4
and
t2.
month
=
'2012-02'
and
t2.amount != 0
and
t2.id
is
null
|
##where or and 區別
# --->22條記錄
###更為強大的臨時表查詢功能,將以上查詢結果作為一個整體放入。
##跟用戶部門中間表關聯,按部門id排序顯示。
1
2
3
|
select
t4.userid
from
(
select
*
from
(
select
userid
from
bbscs_role_user
where
roleid =
'sales'
) t2
left
join
(
select
refid
from
bbscs_sales_income_stat
where
type = 4
and
month
=
'2012-02'
and
amount != 0) t1
on
t2.userid = t1.refid
WHERE
t1.refid
is
null
) t3, bbscs_org_user t4
where
t3.userid = t4.userid
order
by
orgId
|