简体   繁体   中英

Rows that are in table A but not in B

如何检查表 A 中 a、b、c 和 d 列而不是表 B 中的行?

select a.a,a.b,a.c,a.d
  from tablea as a
except
select b.a,b.b,b.c,b.d
  from tableb as b

If you want to get full rows from table a you can use exists :

select *
from tbl_a
where not exists (
    select *
    from tbl_b
    where tbl_b.a = tbl_a.a
    and   tbl_b.b = tbl_a.b 
    and   tbl_b.c = tbl_a.c 
    and   tbl_b.d = tbl_a.d
)

You can LEFT JOIN the table B to get the common elements between both tables plus the remaining in A (if any field doesn't match the join, B will be NULL ), and then remove the inner part by filtering only the NULL values for B

SELECT A.*
FROM A
LEFT JOIN B
    ON A.a = B.a
   AND A.b = B.b
   AND A.c = B.c
WHERE COALESCE(B.a, B.b, B.c) IS NULL;

fiddle

How can I check the rows for columns a, b, c, and d in table A but not in table B?

This will show you the total values for all the columns you indicated in A as well as row data

SELECT
 a.a
,a.b
,a.c
,a.d
,b.e
FROM A as a
LEFT JOIN B as b
ON a.a = a.b
Where   b.a IS NULL 
    and b.b IS NULL  
    and b.c IS NULL
    and b.d IS NULL
GROUP BY GROUPING SETS ((),(a.a,a.b,a.c,a.d))
ORDER BY a.a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM