Home > OS >  How to use UNION ALL without null values for column count mismatch
How to use UNION ALL without null values for column count mismatch

Time:03-30

I have below tables

select id,name from t1

id  name
1   A
2   B

select id,name,sal from t2

id name sal
1   C   1000
2   D   2000

select id,name,Null as sal from t1 
UNION ALL
select id,name,sal from t2

id  name sal
1   A   null
2   B   null
1   C   1000
2   D   2000

But I need like this without null can we use union all option

id  name sal
1   A
2   B
1   C   1000
2   D   2000

CodePudding user response:

One way you can try to add an empty string for T1, let sal's type as a string.

SELECT id,name,'' sal
FROM T1
UNION ALL
SELECT id,name,sal::TEXT
FROM T2
  • Related