[英]Problem with comparing two sql queries with EXCEPT
我有兩個疑問。 第一個查詢從數據庫返回 4554 個結果,第二個查詢返回 3830 個結果。 我需要獲取並列出兩個查詢之間的差異的 724 個結果,這些結果存在於第一個查詢中,不存在於第二個查詢中。 我嘗試使用 EXCEPT 函數,但在控制台中出現錯誤
FOR, GROUP, HAVING, INTO expected got 'EXCEPT'
任何幫助表示贊賞。 這是我的疑問。
select * from billing_trans B,members M where (sign>='2021-03-01 00:00:00' && sign<='2021-03-31 23:59:59') AND M.id=B.mid
EXCEPT
select * from billing_trans B,members M where (sign>='2021-03-01 00:00:00' && sign<='2021-03-31 23:59:59') AND M.id=B.mid
AND (trans LIKE 'BH%' OR bank IN ('SM', 'TO', 'II'));
您可以使用not exists
。 大概你打算:
select *
from dt_billing_trans B join
dt_members M
on M.id = B.mid
where signD >= '2021-03-01' and signD < '2021-04-01' and
not exists (select 1
from dt_billing_trans b2
where b2.mid = b.mid and
b2.signD >= '2021-03-01' and
b2.signD < '2021-04-01' and
(b2.transId LIKE 'WH%' OR b2.bank IN ('WT', 'MO', 'SL'))
);
這將返回沒有符合第二組條件的交易的成員的所有交易。
這並不完全等同於您的代碼( except
在任何數據庫中都不需要),但我懷疑它更接近您的意圖。
筆記:
JOIN
。 JOIN
。 JOIN
。AND
布爾運算符是AND
,而不是&&
。您不需要 2 個查詢。
使用 1 個WHERE
子句:
SELECT *
FROM dt_billing_trans B INNER JOIN dt_members M
ON M.id = B.mid
WHERE (signD >= '2021-03-01 00:00:00' AND signD <= '2021-03-31 23:59:59')
AND NOT (transId LIKE 'WH%' OR bank IN ('WT', 'MO', 'SL'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.