2017-03-06 81 views
0

有没有办法减去基于条件的数据集?减少某些condtiton

如:

表A:

id Count 
1 2 
2 1 
3 4 

表B:

id Count 
1 2 
2 1 
3 5 

我想表A MINUS表B,即使B.Count >= A.Count为各个ID为常我们基于相等具有MINUS(设置操作)操作。

回答

2

尝试

select A.* 
from A 
where not exists (
    select id 
    from B 
    where A.id=B.id and A.count <= B.count) 
0
SELECT B.count - A.count as result 
FROM TABLEB B 
JOIN TABLEA A on A.id = B.id 
WHERE B.count >= A.count