select id , '' as employee_id from table1
union
select id , emp_id as employee_id from table2
但是我从两个加入的联合查询中得到了employee_id数据类型不兼容的错误。那么如何解决这个问题。
emp_id是数字数据类型
如果要字符串,请转换为字符串:
select id , cast('' as varchar(255)) as employee_id from table1
union all
select id , cast(emp_id as varchar(255) as employee_id from table2;
通常,您将使用NULL
而不是''
,因为它与更多类型兼容:
select id , NULL as employee_id from table1
union all
select id , emp_id as employee_id from table2;
注意,我将union
更改为union all
。 union
会导致删除重复项的开销。仅在您希望增加开销的情况下使用它。