declare @sqlstr varchar(max), @querywhere varchar(max)
--set @querywhere='where BarCode like ''%9%'''
--set @querywhere='where BarCode =''9'''
set @querywhere='where s.StoreNo =''234'' '
set @sqlstr='
select distinct 0 as ttype,''店'' as ttype1, s.Id,s.StoreNo as tNo,s.StoreName as tName,s.Provinces,s.City,s.Region
from ST_Store s
left join ST_StoreShelf as ss on (ss.S_Id=s.Id)
left join ST_ShelfReserve as sr on (sr.S_Id=ss.Id)
left join CO_Commodity as c on(sr.C_Id=c.Id),ST_Depot d '+@querywhere+'
union
select distinct 1 as ttype,''仓库'' as ttype1,d.Id,d.DepotNo as tNo,d.DepotName as tName,d.Provinces,d.City,d.Region
from ST_Depot d
left join ST_DepotReserve dr on (dr.D_Id=d.Id)
left join CO_Commodity c on (c.Id=dr.C_Id),ST_Store s '+@querywhere
exec(@sqlstr)
这是运行结果:
union是联合查询,你在后面把仓库表里面的值也查询了
union 后面加个all试试
union是什么意思,你知道吗?
就是把两个查询结果拼接起来。你先查询店,然后查询仓库,当然是包含了仓库的信息。除非没有满足条件的仓库。如果你只想查询店的信息,就没有必要写仓库的相关代码?union后面的没有必要写吧。
举个例子:
a的内容:
a1
a2
a3
b的内容
b1
select * from a, b
结果:
a1 b1
a2 b1
a3 b1
select * from a, b where b = b1
结果还是
a1 b1
a2 b1
a3 b1
原因两个表没有连接条件,过滤条件是连接完以后再过滤的。
你在连接ST_Store s时也是这个问题,没有连接条件只有过滤条件。
期望该回答对你有点小帮助!