-1

我试图将结果组合到一个输出中,其中一列是DepositNo,另一列是Doc_Type任何Doc_Type等于DocType(反之亦然),显示DepositNoof DocType

这是我的查询:

SELECT Doc_Type
  FROM Doc_Outgoing_Details
 WHERE (Doc_Type <> 67)
UNION
SELECT DepositNo, DocType AS Doc_Type
  FROM Doc_Outgoing
 WHERE (DocType <> 67)

当我执行它时,它说:

UNION 的目标列表中必须有相同数量的表达式。

4

4 回答 4

1

当您使用联合时,您应该匹配所有列,并且您的结果集应该具有相同数量的列,因此您也需要在第一个查询中生成 Doc_Type 列:

SELECT        Doc_Type,0 as Doc_Type  // or every value 
                                      //that you didn't use in real DOCTYPE
FROM            Doc_Outgoing_Details
WHERE        (Doc_Type <> 67)
UNION
SELECT        DepositNo, DocType AS Doc_Type
FROM            Doc_Outgoing
WHERE        (DocType <> 67)
于 2013-03-10T11:38:31.973 回答
1

因为您的报表上投影的列数SELECT不匹配。第一个包含一列,第二个包含两列。尝试在第一个查询中添加空值,

SELECT    Doc_Type as DepositNo, NULL Doc_Type
FROM      Doc_Outgoing_Details
WHERE     Doc_Type <> 67
UNION
SELECT    DepositNo, DocType AS Doc_Type
FROM      Doc_Outgoing
WHERE     DocType <> 67
于 2013-03-10T11:38:50.933 回答
1

您正在寻找的是INNER JOIN.

SELECT d.DepositNo, D.Doctype
FROM Doc_Outgoing d
INNER JOIN Doc_Outgoing_Details do
    ON d.Doctype = do.Doc_type
WHERE (d.DocType <> 67)
于 2013-03-10T11:39:56.837 回答
1

你的第二个专栏缺少一列tableUNION必须有相等数量的columns。这应该有效。

SELECT        O AS DepoSitNo, Doc_Type
FROM            Doc_Outgoing_Details
WHERE        (Doc_Type <> 67)
UNION
SELECT        DepositNo, DocType AS Doc_Type
FROM            Doc_Outgoing
WHERE        (DocType <> 67)
于 2013-03-10T11:41:25.683 回答