Multi table search
Multi table search
(OP)
I have 3 tables (A,B,C) and each has an "id" column. I'm trying to create a query that will tell me if any of the three tables has a row where id is a specific value. I could do this as 3 separate queries but I feel like I should be able to do it as one.
RE: Multi table search
CODE
SELECT 'A' TableName, ID FROM TableA
UNION SELECT 'B', ID FROM TableB
UNION SELECT 'C', ID FROM TableC
) U
WHERE id='specific value'
Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?
RE: Multi table search
RE: Multi table search
since you are only interested in whether the particular id value is present, and not which table it came from, you can omit those
CODE
FROM (
SELECT ID FROM TableA
UNION
SELECT ID FROM TableB
UNION
SELECT ID FROM TableC
) U
WHERE id = specificvalue
r937.com | rudy.ca
Buy my new book Simply SQL from Amazon