vb.net 一个联合查询的两个选定表或查询中的列数不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25580929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The number of columns in the two selected tables or queries of a Union query do not match
提问by Keval savani
I have been facing error in MS Access and error is "The number of columns in the two selected tables or queries of a Union query do not match."
我在 MS Access中遇到错误,错误是“联合查询的两个选定表或查询中的列数不匹配”。
Here is my SQL query:
这是我的 SQL 查询:
SELECT sale_head.suppliername AS sale_head_suppliername,
sale_head.invoiceno AS sale_head_invoiceno, sale_head.invoicedate,
sale_details.invoiceno AS sale_details_invoiceno, sale_details.suppliername AS sale_details_suppliername,
sale_details.product_code, sale_details.qty, sale_details.totalkg, sale_details.Rate, sale_details.subtotal FROM sale_head
INNER JOIN sale_details ON sale_head.[invoiceno] = sale_details.[invoiceno]
UNION ALL select 'Total', sum(sale_details.subtotal) from sale_details
WHERE (((sale_head.suppliername)='Ramkrishna Creation'));
Am I missing something ? If yes please do let me know.
我错过了什么吗?如果是,请告诉我。
回答by Beatles1692
When you union two or more queries together each query should have the same columns of data with same data type for example :
当您将两个或多个查询结合在一起时,每个查询都应具有相同数据类型的相同数据列,例如:
SELECT Name,LastName,SUM(Salary) FROM tabel1
UNION
SELECT Text1,Text2,SomeMoney FROM table2
is valid (assuming that Name and Text1,LastName and Text2 and Sum of salary and SomeMoney have the same data type but :
是有效的(假设 Name 和 Text1、LastName 和 Text2 以及 Sum ofsalary 和 SomeMoney 具有相同的数据类型,但是:
SELECT Name,LastName,SUM(Salary) FROM tabel1
UNION
SELECT Text1,SomeMoney FROM table2
(cloumns count mismatch)or
(克隆计数不匹配)或
SELECT Name,LastName,SUM(Salary) FROM tabel1
UNION
SELECT Text1,SomeMoney,Text2 FROM table2
(data type mismatch)are not valid union statements.
(数据类型不匹配)不是有效的联合语句。
UPDATE : My answer is according to SQL Standard Definition of Union Statementwhich states :
更新:我的回答是根据SQL Standard Definition of Union Statement规定:
The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
UNION 运算符用于组合两个或多个 SELECT 语句的结果集。
请注意,UNION 中的每个 SELECT 语句必须具有相同的列数。列也必须具有相似的数据类型。此外,每个 SELECT 语句中的列必须具有相同的顺序。
回答by Matthew
In a UNION, both datasets must have the same number of columns but they don't need to be the same datatype
在 UNION 中,两个数据集必须具有相同的列数,但它们不需要是相同的数据类型
All queries in a UNION operation must request the same number of fields; however, the fields do not have to be of the same size or data type.
UNION 操作中的所有查询必须请求相同数量的字段;但是,字段不必具有相同的大小或数据类型。

