简体   繁体   English

从一个表中获取一列,而从另一表中获取两列

[英]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 (反之亦然),该DepositNoDocType显示。

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 of columns . 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.

相关问题 mysql-表的两列和另一列的计数 - mysql - count of two columns of a table and one column from another 将数据从一列插入另一表的两列的过程 - Procedure to insert data from one column into two columns in another table 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns 如何从一个表中获取所有列,而从另一张表中仅获取具有ID的一列? -MySQL - How to get all columns from one table and only one column from another table with ID ? - MySql 如何在 SQLITE 中从一个表中获取所有列并从另一表中获取一列 - How to get all columns from one table and one column from another table in SQLITE 将一个表中的两列连接到另一引用表中的列 - Join two columns in one table to a column in another reference table sql两个列的一个表引用另一个表中的同一列 - sql two columns of one table reference same column in another table 将一个表的两列连接到另一表的同一列 - Join two columns of one table to the same column of another table 如何将一个列数据从一个表中获取到另一个表中作为PL / SQL中的不同列 - how to get one column data from one table into another table as different columns in PL/SQL 从一个表到另一表获取列 - Get columns from one table to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM