tags:

views:

31

answers:

1

I need a query statement written such that it searches three tables

table 1

id
fullname
active

table 2

id   
fullname 

table 3

id 
fullname

I wanted this query to search all three tables combine and give me the result for those id fullname that is not active

+1  A: 

Assuming that all tables have the same structure (in your example they do not - is this an error?) then you can use UNION ALL to combine results from multiple queries:

SELECT id, fullname, 'table1' AS source
FROM table1
WHERE active = 'N'
UNION ALL
SELECT id, fullname, 'table2' AS source
FROM table2
WHERE active = 'N'
UNION ALL
SELECT id, fullname, 'table3' AS source
FROM table3
WHERE active = 'N'

If you don't care where the rows come from you and you don't want duplicates you could try UNION instead:

SELECT id, fullname
FROM table1
WHERE active = 'N'
UNION
SELECT id, fullname
FROM table2
WHERE active = 'N'
UNION
SELECT id, fullname
FROM table3
WHERE active = 'N'

I'm making a lot of guesses here. If it doesn't work, please clarify what you are trying to do.

Mark Byers