我有两个表 ItemsDetails 和 ItemsSquare
**ItemsDetails** table have following column
ItmDtlId P.K
Itmid F.K
AssetId F.k
Qty
TDate
Approved
**ItemsSquare** table have following column
ItmSqrId P.k
ItmDtlId F.k
ItmSqQty
Date
我要显示 ItemsDetails 表中的所有记录,也要显示 ItemsSquare 表中的记录 ItmDtlId与 ItemDetails 中的相同,然后将“ ItemDetails”表和“ Itemsquare”表中的“数量”作为“数量”>“ ItmSqQty
”进行比较。基本上我想基于ItmDtId访问 ItemDetails 表中的 ItemsSquare 表中的列 因为第一张表与第二张表没有关系
我正在按照以下方式使用sql Correlated子查询,但是我没有得到预期的结果
这是我的SQL查询
SELECT itd.ItmDtlId
, it.Itmid
, itd.Qty
, itd.Approved
, as.Assetid
, as.Assetname
, itd.TDate
from ItemsDetails itd
join Item it
on itd.Itmid = it.Itmid
join Assets as
on itd.Assetsid = as.Assetsid
WHERE itd.Approved = 1
and itd.ItmDtlId = (SELECT itd.ItmDtlId FROM ItemsSquare its WHERE itd.ItmDtlId = its.ItmDtlIdand itd.Qty > ItmSqQty)
请建议我如何有效地编写Sql子查询以获得所需结果
答案 0 :(得分:0)
是否可能错误地引用了子查询?
代替:
...
AND itd.ItmDtlId = (
SELECT itd.ItmDtlId
FROM ItemsSquare its
WHERE itd.ItmDtlId = its.ItmDtlIdand
AND itd.Qty > its.ItmSqQty
)
收件人:
...
AND itd.ItmDtlId = (
SELECT its.ItmDtlId
FROM ItemsSquare its
WHERE itd.ItmDtlId = its.ItmDtlIdand
AND itd.Qty > its.ItmSqQty
)