user4704802
user4704802

Reputation:

oracle 11g beginner - how to display data from two tables

Complete beginner to Oracle 11g here.

If I have two tables, with some overlapping attributes but no relation, how do I display all the data in one table?

Example:

TABLE A has a PK, a fname, lname.  
TABLE B has a PK, a fname, lname, phone.

I'd like to display the contents of TABLE A and TABLE B in one table.

I thought I could use a UNION, but you have to have the same number of columns; and since TABLE B has 3 columns, I get an error: ORA-01789: query block has incorrect number of result columns.

Upvotes: 1

Views: 141

Answers (1)

jpw
jpw

Reputation: 44881

You could insert a "fake" column into the select for TableA so that two two sets line up:

select PK, fname, lname, null as phone from TableA
union all 
select PK, fname, lname, phone from TableB

This would effectively merge the two tables.

Upvotes: 2

Related Questions