如何使用整数数据类型列硬编码整数数据类型联合的空白值

how to have hardcoded blank value of integer data type union with integer datatype column

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