2

How can I run this SQL query multiple times in a loop, where I replace the word 'pubs' with another word during each iteration. Is there a way to store an array of strings and loop through them?

SELECT * FROM businesses WHERE category='pubs'
1
  • What programming language? Commented Aug 11, 2016 at 23:05

2 Answers 2

5

In general, it's usually better performance-wise to do bulk or batch queries than queries in a loop, since you can save round trip calls to the DB.

Consider instead doing something like SELECT * from businesses WHERE category IN ('pubs', ...), or if you plan to iterate over all categories, retrieve all item rows and programmatically use category in the returned models to do what you need to with them.

If you absolutely must use a loop, you can look at the loop documentation.

Sign up to request clarification or add additional context in comments.

Comments

2

You probably don't need a loop to run them rather use a IN clause to include all the possible condition values like

SELECT * FROM businesses WHERE category IN ('pubs','subs','nubs')

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.