Merge two SELECT statements which have one or more additional columns

时间:2015-05-12 22:27:25

标签: sql select merge

I have two select statements like below

ALTER TABLE tracks DROP COLUMN album_name;
ALTER TABLE tracks DROP COLUMN author_name;

gives me

SELECT * FROM TABLE1 

and

COLUMNA, COLUMNB, COLUMNC

gives me

SELECT * FROM TABLE2

COLUMNA, COLUMNB are identical(same number of rows and cell values) in those two SELECTs

How can I merge these two SELECTs so I can have four columns with one query and no extra rows

COLUMNA, COLUMNB, COLUMND

Updating my question based on comments. Let's say I have two tables like below

COLUMNA, COLUMNB, COLUMNC, COLUMND

result should be

TABLE1                     TABLE2
COLUMNA COLUMNB COLUMNC    COLUMNA COLUMNB COLUMND
value1  value2  value3     value1  value2  value9
value4  null    value5     value4  null    value10
null    value6  value7     null    value6  value11
null    null    value8     null    null    value12

4 个答案:

答案 0 :(得分:2)

I guess you're looking for something like this.

.dot

or maybe:

SELECT COLUMNA, COLUMNB, COLUMNC, NULL as COLUMND FROM TABLE1 

UNION 

SELECT COLUMNA, COLUMNB, NULL as COLUMNC, COLUMND FROM TABLE2

答案 1 :(得分:2)

This will give you 3 columns, the third being the different valued one:

operator(Base, Base)

答案 2 :(得分:2)

Try this:

\

And if there is a row that are not identicall in columnA and columnB or not exists, and you want it on the result use:

SELECT A.COLUMNA, A.COLUMNB, A.COLUMNC, B.COLUMND 
FROM TABLE1 A
JOIN TABLE2 B ON A.COLUMNA = B.COLUMNA AND A.COLUMNB = B.COLUMNB

答案 3 :(得分:1)

Since you indicate the rows and values for addAsk() and ColumnA are identical, you can ColumnB the two tables together if the values are the same

JOIN

If you have disparity between the two tables, i.e. some records in one but not the other, you can use SELECT a.ColumnA ,a.ColumnB ,a.ColumnC ,b.ColumnD FROM Table1 a JOIN Table2 b ON a.ColumnA = b.ColumnA AND a.ColumnB = b.ColumnB and change the FULL JOIN:

SELECT