Yash
Yash

Reputation: 1436

Select a dummy column with a dummy value and search in dummy column in SQL?

I have two table which I need to join

Table1
col1   col2   col3 
------------------
 1      A      20 
 2      B      10
 3      C      30
 4      D      40

Table2
col1   col2
-----------
 1      A  
 2      B  
 3      C  
 4      D  

I have to make UNION of both table. As table2 doesn't have col3 I have added as dummmy column in query.

I need to perform search operation in both the table for col3

I tried but it throwing unknown column exception.

(SELECT col1, col2, col3 from table1 where col3 = "10")
 UNION
(SELECT col1, col2, "10" as col3 from table2 where col3 = "10")

Any help would be appreciated

Upvotes: 0

Views: 180

Answers (2)

Ken White
Ken White

Reputation: 125718

You can query the UNIONed data directly afterward:

SELECT col1, col2, col3 from
(
 (SELECT col1, col2, col3 from table1)
  UNION
 (SELECT col1, col2, "10" as col3 from table2)
) as b 
where col3 = "10"

Upvotes: 1

forpas
forpas

Reputation: 164139

There is no need for a WHERE clause in Table2:

SELECT col1, col2, col3 FROM Table1 WHERE col3 = '10'
UNION 
SELECT col1, col2, '10' FROM Table2

See the demo.
Results:

> col1 | col2 | col3
> ---: | :--- | :---
>    2 | B    | 10  
>    1 | A    | 10  
>    3 | C    | 10  
>    4 | D    | 10 

Upvotes: 0

Related Questions