我有两张表,a表和b表 想按照id和输入时间进行比较如果a.id = b.id and a.sj <> b.sj 就获取这条数据, 但因为这两张表没有关联,我的查询语句不管怎么写都不对, 求大神帮忙..
select * from a where exists (select 1 from b where a.id = b.id and a.sj <> b.sj) 这是我原来写的sql , 查询的数据总会比我想要查的多.
------解决方案--------------------
语句看着没什么问题
可以先查一下 a b 两个表中是否有ID或时间为空的数据
这类数据可能会造成查询的范围扩大
------解决方案--------------------
select * from a a where exists (select 1 from b b where a.id=b.id and a.sj<>b.sj)
and not exists (select 1 from b b where a.id=b.id and a.sj=b.sj)
这样?