mysql 一张表减另一张表

版权声明:此文章为许诗宇所写,如需转载,请写下转载文章的地址 https://blog.csdn.net/xushiyu1996818/article/details/82497243

前提是双方数据结构相同,至少得用一个外键
查询表1-表2的数据
就是查询 表1 中 (主键 不在表2)的数据
用not exists
后面的select* 的结果如果为空,则为false
有数据,即主键存在,在为true

SELECT id,
 total,
 now_use,
 next_use,
 now_return,
 next_return,
 now_balance,
 next_balance,
 total - next_balance
FROM
    `user`
WHERE
    now_use <> 0
AND next_balance < total
AND next_use = 0
AND NOT EXISTS (SELECT * FROM owe_user WHERE user.id = owe_user.id);

猜你喜欢

转载自blog.csdn.net/xushiyu1996818/article/details/82497243