Comparing a column against an entire table of ranges?
Sorry if this has been asked before, I've searched and have been unable to find any answers.
I have 2 tables in a DB, TableA has a column we'll call ID (a 4-digit integer), and TableB has 2 columns o开发者_Go百科f 4-digit integers that form a range.
What I would like to do is check to see which ID's can be found in any range, so that if ID had
1 5 7 9, and TableB had ranges 4-6, 100-101, and 3000-4000, 5 would be properly matched as belonging to a range. Any ideas?
Try this:
SELECT *
FROM TableA a
WHERE EXISTS
(
SELECT 1
FROM TableB b
WHERE a.ID BETWEEN b.ID1 AND b.ID2
)
精彩评论