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删除重复项会产生开销。仅当您想产生该开销时才使用它。