我有3个表,我想要联合所有,但只有两列能够显示数据。
SELECT Vehicle_ID, Total_Weight, date, bilty_No FROM harvesting
UNION
SELECT Vehicle_ID, Total_Weight, date, Bilty_No FROM Blade_Harvesting
我想添加另一个表,如下面但是给出错误
SELECT Vehicle_ID, Total_Weight, date, bilty_No FROM harvesting
UNION
SELECT Vehicle_ID, Total_Weight, date, Bilty_No FROM Blade_Harvesting
UNION
SELECT Vehicle_ID, Vehicle_Name FROM Vehicle
发出此错误:
使用UNION,INTERSECT或EXCEPT运算符组合的所有查询在目标列表中必须具有相同数量的表达式
答案 0 :(得分:1)
您必须在所有UNION SELECT子句中指定相等的列:
SELECT Vehicle_ID VehicleId, Total_Weight TotalWeight,date,bilty_No
biltyNo,'' VehicleName
FROM harvesting
UNION ALL
SELECT Vehicle_ID VehicleId, Total_Weight TotalWeight,date,Bilty_No
biltyNo ,'' VehicleName
FROM Blade_Harvesting
UNION ALL
SELECT Vehicle_ID VehicleId ,0 TotalWeight,'' date ,'' biltyNo
,Vehicle_Name VehicleName
FROM Vehicle
答案 1 :(得分:0)
UNION
中的每个SELECT语句必须具有相同的列数。
列还必须具有类似的数据类型。
每个SELECT语句中的列也必须具有相同的顺序
试试这个
SELECT Vehicle_ID,cast(Total_Weight as varchar),date,bilty_No FROM harvesting
UNION
SELECT Vehicle_ID,cast(Total_Weight as varchar),date,Bilty_No FROM Blade_Harvesting
union
select Vehicle_ID,Vehicle_Name,'','' from Vehicle
答案 2 :(得分:0)
请用户:
SELECT Vehicle_ID,Total_Weight,date,Bilty_No,''AS Vehicle_Name FROM harvesting
UNION SELECT Vehicle_ID,Total_Weight,date,Bilty_No,''AS Vehicle_Name FROM Blade_Harvesting
UNION SELECT Vehicle_ID,'0'AS Total_Weight,NULL AS date,'0'AS Bilty_No,Vehicle_Name FROM Vehicle