1

I'm querying a table that returns id's and I'd like to use these id's within an array to perform another query.

I have the two queries working independently of each other by hard coding the id's into the second query just to be sure that it functions as expected, which it does.

$query = "SELECT item_id FROM items"; 

$query = "SELECT * FROM results WHERE results_item_id in (1,2,3,4)";  

I'd like the return of the second query to include data for all of the id's returned to me from the first query.

2 Answers 2

2

Instead of sub query, you can do it by JOIN matching ON item_id = results_item_id

$query = "SELECT R.* FROM results R JOIN items I ON I.item_id = R.results_item_id";  
Sign up to request clarification or add additional context in comments.

Comments

1

Just put the query inside the IN, and add distinct so each item_id you only get in once:

$query = "SELECT * FROM results WHERE results_item_id in (SELECT distinct item_id FROM items)";  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.