两个表差异数据比对SQL

结果集1
select id,status,amount,create_time,callback_time
from tmp_b where create_time>’2023-05-06′ and accountId=’xxx’ and status=’SUCCESS’

结果集2
select a FROM tmp_a

找出结果集1在结果集2中不存在的记录

SELECT f.id, f.status, f.amount, f.create_time, f.callback_time
FROM tmp_b f
LEFT JOIN tmp_a a ON f.id = a.a
WHERE f.create_time > '2023-05-06'
  AND f.accountId = 'xxx'
  AND f.status = 'SUCCESS'
  AND a.a IS NULL;
SELECT id, status, amount, create_time, callback_time
FROM tmp_b
WHERE create_time > '2023-04-25' and create_time<'2023-05-07'
  AND accountId = 'xxx'
  AND status = 'FAILURE'
  AND id NOT IN (
    SELECT a
    FROM tmp_a
  );

找出结果集2在结果集1中不存在的记录

SELECT a AS id, NULL AS status, NULL AS amount, NULL AS create_time, NULL AS callback_time
FROM tmp_a
WHERE a NOT IN (
  SELECT id
  FROM tmp_b
  WHERE create_time > '2023-04-25' and create_time<'2023-05-07'
    AND accountId = 'xxx'
    AND status = 'FAILURE'
);

 

发表评论

邮箱地址不会被公开。