[英]Get one column from one table with two columns of another table
I'm trying to combine the results in one output where one column is DepositNo
and the other is Doc_Type
where any Doc_Type
equals to DocType
(vise versa), the DepositNo
of DocType
is displayed. 我想其中一个列的结果在一个输出组合
DepositNo
,另一个是Doc_Type
其中任何Doc_Type
等于DocType
(反之亦然),该DepositNo
的DocType
显示。
Here's my query: 这是我的查询:
SELECT Doc_Type
FROM Doc_Outgoing_Details
WHERE (Doc_Type <> 67)
UNION
SELECT DepositNo, DocType AS Doc_Type
FROM Doc_Outgoing
WHERE (DocType <> 67)
When I execute it, it says: 当我执行它时,它说:
UNION must have and equal number of expressions in their target list.
UNION在其目标列表中必须具有相等数量的表达式。
when You use union you should match all columns and your result sets should have same numbers of columns so you need to generate Doc_Type column in your fist query too : 当您使用并集时,您应该匹配所有列,并且结果集应具有相同数量的列,因此您也需要在第一查询中生成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)
Because the number of columns projected on your SELECT
statement doesn't match. 因为投影在
SELECT
语句上的列数不匹配。 The first one contains one column and other second one has two. 第一个包含一个列,其他第二个包含两个列。 Try adding null value on the first query,
尝试在第一个查询中添加空值,
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
What you are looking for is INNER JOIN
. 您正在寻找的是
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)
you are missing a column on your second
table
.您在第二张
table
上缺少一列。UNION
must have equal number ofcolumns
.UNION
必须具有相等的columns
。 This should work.这应该工作。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.