mysql - sql 将列添加到结果中

标签 mysql sql sql-server

所以我希望将 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/

相关文章:

mysql - MySQL docker容器的性能问题

php - 将android连接到免费虚拟主机中的服务器

MySQL INFORMATION_SCHEMA 表未填充

c# - EF 中的 CurrentDateTime()

sql-server - WCF 和 SQL Server - 如何处理数据变化?

sql-server - XML作为存储过程中的参数(sql server)

c# - 在 Entity Framework 4.0 中显示 "Nested transactions are not supported"错误?

sql - 在具有硬连线预分配变量的系统上配置 SQL Server 动态 SQL "IN clause"

mysql - 返回所有用户的列表,与他们最多的 "popular"关注者配对。某人拥有的追随者越多,他们的 "popular"就越多

MySQL - 选择具有最大时间戳的唯一列