一条查询语句?
语句如下:
SELECT dbo.StorageInOutInfo.Guid, dbo.StorageInOutInfo.Type, dbo.StorageInOutInfo.CreateTime, dbo.StorageInOutInfo.GameAccountPartGuid,
dbo.StorageInOutInfo.FromGuid, dbo.StorageInOutInfo.TotalPrice, dbo.GameAccountPartInfo.GameServerCode, dbo.GameServerInfo.CName, dbo.GameAccountPartInfo.Nickname, dbo.StockOrderInfo.Code
FROM dbo.GameAccountPartInfo INNER JOIN
dbo.StorageInOutInfo ON dbo.GameAccountPartInfo.Guid = dbo.StorageInOutInfo.GameAccountPartGuid INNER JOIN
dbo.GameAccountInfo ON dbo.GameAccountPartInfo.GameAccountGuid = dbo.GameAccountInfo.Guid INNER JOIN
dbo.StockOrderInfo ON dbo.StorageInOutInfo.FromGuid = dbo.StockOrderInfo.Guid INNER JOIN
dbo.SaleOrderInfo ON dbo.StorageInOutInfo.FromGuid = dbo.SaleOrderInfo.Guid
这个是查不出结果,并且错误。
因为dbo.StorageInOutInfo.FromGuid 允许为空, 而dbo.StorageInOutInfo.FromGuid = dbo.StockOrderInfo.Guid, 错误,
就算添加了这句WHERE(dbo.StorageInOutInfo.FromGuid <> ' '), 还是查不出,因为
dbo.StorageInOutInfo.FromGuid = dbo.StockOrderInfo.Guid(会取出StorageInOutInfo.Type = 0 所有记录,而 =1 的记录没有)
和
dbo.StorageInOutInfo.FromGuid = dbo.SaleOrderInfo.Guid(会取出StorageInOutInfo.Type = 1 所有记录,而 =0 的记录没有)
所以这两条inner join,取结果为空。
我现在要取的是 StorageInOutInfo.Type为0和为1 的所有记录,以及StorageInOutInfo.FromGuid 为空的全部都显示,并且在后面加
字段Code,是由SaleOrderInfo.Code和StockOrderInfo.Code取得,如果StorageInOutInfo.FromGuid 为空的话,那么追加的Code也为空值填充。
请各位SQL 高手 帮帮忙,解决小弟一下。谢谢
------解决方案--------------------楼主不确定看起来累吗?
引用表名时用别名如:
dbo.表名 as a join dbo.表名b as b
SELECT
b.Guid,
b.Type,
b.CreateTime,
b.GameAccountPartGuid,
b.FromGuid,
b.TotalPrice,
a.GameServerCode,
dbo.GameServerInfo.CName, ---这是那一个表的列?
a.Nickname,
e.Code
FROM
dbo.GameAccountPartInfo a
INNER JOIN
dbo.StorageInOutInfo b
ON a.Guid = b.GameAccountPartGuid
INNER JOIN
dbo.GameAccountInfo c
ON a.GameAccountGuid = c.Guid
INNER JOIN
dbo.StockOrderInfo d
ON b.FromGuid = d.Guid
INNER JOIN
dbo.SaleOrderInfo e
ON b.FromGuid =e.Guid