acolls_badger
acolls_badger

Reputation: 463

Create Object From SQL Selection

I want to create a list of objects from a SELECT query (or something like that) on one table, to then apply this to another table to return matching rows.

My Code:

--Create an object from a selection.

SELECT (item_number) AS the_item_numbers,
FROM Table_1
WHERE CATEGORY = 'A Category in Table_1.Category'

--Now I want to use the object the_item_numbers to return every matching row from a separate table.

SELECT *
WHERE item_number IN (the_item_numbers)
FROM Table_2

Currently my only method is to use the select query to get a list of item_numbers, that I then manually add to the IN of the second select query, which I'm sure is an unnecessarily slow way to do it.

How can I achieve the final result in one query?

Upvotes: 0

Views: 2363

Answers (1)

Miniver Cheevy
Miniver Cheevy

Reputation: 1677

The easiest way would be a join

Select Table_2.* 
From Table_2 
Inner Join Table_1 on 
Table_1.ItemNumber = Table_2.ItemNumber
Where Table_1.Category = 'A Category in Table_1.Category'

Upvotes: 1

Related Questions