所以我希望将 itemlookupcode
添加到查询结果中。 itemlookupcode
位于项目表中,但查询针对不同的表。两个表都有 itemdescription
,但是当我进行联接或左联接时,它会出现重复项。
select *
from [RAPurchaseOrderTransfer]
where QtyDifference <> 0
and fromstoreid = 111
and DateCreated >= dateadd(dd, -30, GETDATE())
有什么想法可以将 item.lookupcode
添加到结果中吗?
最佳答案
为了避免重复,您可以执行嵌套选择来获取 itemlookupcode。
对于 MySQL,它看起来像这样(限制 1 将仅返回 1):
(select i.itemlookupcode from item i where i.itemdescription = r.itemdescription limit 1)
对于 SQL Server,它看起来像这样:
(select top 1 i.itemlookupcode from item i where i.itemdescription = r.itemdescription)
例如:
select
r.*,
(select top 1 i.itemlookupcode from item i where i.itemdescription = r.itemdescription)
from [RAPurchaseOrderTransfer] r
where r.QtyDifference <> 0
and r.fromstoreid = 111
and r.DateCreated >= dateadd(dd, -30, GETDATE())
关于mysql - sql 将列添加到结果中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26557508/