sql server简单题 急!

来源:百度知道 编辑:UC知道 时间:2024/05/31 20:42:43
有两个表 一个是退货单#a,一个是入库单 #b
分别都有字段 spbh(商品编号)和shl(数量)

要查询#a的spbh不在#b里的 并且数量不相等的
如何写?
二楼。我就是写成你那种 查询后 本来几百条数据变成了几千条

另外,退货单数量为负数 入库单为正数 是否应该在where后加上and a.shl+b.shl<>0

select #a.spbh,#a.shl from #a left join #b on #a.spbh=#b.spbh where #a.shl<>#b.shl or #a.spbh not in (select spbh from #b)

select #a.spbh,#a.shl from #a left join #b on #a.spbh=#b.spbh where #a.shl+#b.shl<>0 or #a.spbh not in (select spbh from #b)
第二个是为负数时数量不相等的语句

你试试

select spbh from #a,#b where #a.spbh not in (select spbh from #b) and #a.shl<>#b.shl

我写的这个应该不是最优的
你先试试
如果数据量很大,估计有点慢

Select #a.* from #a where not exists(select 1 from #b where b#.spbh=#a.spbh)
union all
select #a.* from #a,#b where #a.spbh=#b.spbh and #a.shl<>#b.shl

select spbh,shl from #a where spdh not in(select spdh from #b)
union all
select a.spdh spdh,a.shl shl from #a a,#b b
where a.spdh=b.spdh and a.shl<>b.shl

看不懂你到底什么意思!正如一楼说的spbh不在b里,何来数量相等一说?
下面的是 #a的spbh不在#b里的
select * from #a where spbh not in (select spbh from #b)

下面是#a的spbh在#b里的并且数量不相等的

sele