Oracle UNION different columns

2019-02-28 12:10发布

I have two tables looking something like:

TABLE_1
COL_A (int), COL_B (float), COL_C (float)


TABLE_2
COL_A (int), COL_B (float)

My query is using a UNION to put the results of these tables together, but where TABLE_2 doesn't have a COL_C, I'm looking to put something like '0'. But I just get a 'ORA-00918: column ambiguously defined' error

How can I get around this?

2条回答
男人必须洒脱
2楼-- · 2019-02-28 12:46
SELECT COL_A,COL_B,COL_C FROM TABLE_1
UNION
SELECT COL_A,COL_B,'0' AS COL_C FROM TABLE_2

You may also be able to get away with

SELECT COL_A,COL_B,COL_C FROM TABLE_1
UNION
SELECT COL_A,COL_B,'0' FROM TABLE_2

The basic idea is that both must have same number of columns of the same corresponding data types.

查看更多
Root(大扎)
3楼-- · 2019-02-28 12:55

You can try this

SELECT COL_A, COL_B, COL_C FROM Table1
UNION
SELECT COL_A, COL_B, 0 As COL_C FROM Table2
查看更多
登录 后发表回答