jaredw
jaredw

Reputation: 55

How to combine the rows of two different tables into the same column?

I have 2 tables, both with an employee_id and a task_group column. I need to build a query that returns a list of all the task_groups associated with a particular employee_id, whether they exist in either table.

Example:

Table 1:
Emp_ID | Blah_Blah
1234   | junk
6969   | crap

Table 2:
Emp_ID | Blah_Blah
1234   | crud
1234   | poop

And I need to build a query that will return this:

Query:
Blah_Blah
junk
crud
poop

Is this possible?

Upvotes: 2

Views: 157

Answers (2)

pavanred
pavanred

Reputation: 13803

SELECT Blah_Blah from Table1 WHERE <some condition>
UNION
SELECT Blah_Blah from Table2 WHERE <some contition>

Upvotes: 1

Thomas
Thomas

Reputation: 64635

Select blah_blah
From Table1
Where Emp_ID = 1234
Union All
Select blah_blah
From Table2
Where Emp_ID = 1234

If the same value for blah_blah exists in both tables, the above will result in a duplicate. If you want to eliminate duplicates, then use Union instead of Union All:

Select blah_blah
From Table1
Where Emp_ID = 1234
Union 
Select blah_blah
From Table2
Where Emp_ID = 1234

Upvotes: 0

Related Questions