SQL中的选择内连接选择
Posted
技术标签:
【中文标题】SQL中的选择内连接选择【英文标题】:Select Inner Join Select in SQL 【发布时间】:2015-05-21 02:29:24 【问题描述】:您好,我有一个 Select Inner Join 语句,看起来没问题,但我出错了。这是我的查询
ALTER PROCEDURE [dbo].[POBalance] @PONumber nvarchar(50)
AS BEGIN
Select(
Select
A.Description,
C.qty as POqty,
B.QtyDelivered as PDQty,
case when A.partialflag ='false'
then '0'
else
A.qty end as Balance,
A.Unit,
A.Unitprice,
A.Partialflag
from tblPOdetails as A
Inner Join ( SELECT id, SUM(Qty) AS QtyDelivered
FROM dbo.tblPDdetails
WHERE (PONo = @PONumber)
GROUP BY id)as B On A.id = B.id
Inner Join tblpodetailshistory as C on A.id =C.id
where A.PONo = @PONumber)
END
我收到了这个错误。
选择列表中只能指定一个表达式,当 EXISTS 没有引入子查询。
在此先感谢您。
【问题讨论】:
错误信息非常具体地解释了问题是什么以及解决方案将是什么。用非常清晰的措辞您是否阅读了错误信息中的实际文字?哪一部分不清楚? @KenWhite 对不起,我是新手,SQL 我只想在我的第一个表上加入 Qtydelivered ......当我运行子查询时,它似乎没问题。谢谢。 【参考方案1】:您应该在外部选择语句中包含列名和 From 关键字
ALTER PROCEDURE [dbo].[POBalance] @PONumber nvarchar(50)
AS BEGIN
Select Description, POqty, PDQty, Balance, Unit, Unitprice, Partialflag from (
Select
A.Description,
C.qty as POqty,
B.QtyDelivered as PDQty,
case when A.partialflag ='false'
then '0'
else
A.qty end as Balance,
A.Unit,
A.Unitprice,
A.Partialflag
from tblPOdetails as A
Inner Join ( SELECT id, SUM(Qty) AS QtyDelivered
FROM dbo.tblPDdetails
WHERE (PONo = @PONumber)
GROUP BY id)as B On A.id = B.id
Inner Join tblpodetailshistory as C on A.id =C.id
where A.PONo = @PONumber)
END
其实不用写外层select,可以改写成
ALTER PROCEDURE [dbo].[POBalance] @PONumber nvarchar(50)
AS BEGIN
Select
A.Description,
C.qty as POqty,
B.QtyDelivered as PDQty,
case when A.partialflag ='false'
then '0'
else
A.qty end as Balance,
A.Unit,
A.Unitprice,
A.Partialflag
from tblPOdetails as A
Inner Join ( SELECT id, SUM(Qty) AS QtyDelivered
FROM dbo.tblPDdetails
WHERE (PONo = @PONumber)
GROUP BY id)as B On A.id = B.id
Inner Join tblpodetailshistory as C on A.id =C.id
where A.PONo = @PONumber
END
【讨论】:
它可以工作......真的不需要外部选择语句。【参考方案2】:问题出现在查询的开头,
Select( Select ....
删除最初的“Select (”并重试查询。当你运行一个
Select (Select ..)
第二个选择只能返回一列(除非你连接返回的数据)。
【讨论】:
以上是关于SQL中的选择内连接选择的主要内容,如果未能解决你的问题,请参考以下文章