0

I would like to know how I can display multiple rows from database tables? I know one method, but it allows you to display only one line. Here is the code:

('SELECT * FROM cats WHERE cat_id="32" ORDER BY sorting')

I need to tag WHERE show some more id. Bad Example

('SELECT * FROM cats WHERE cat_id="32, 33, 34, 37" ORDER BY sorting')

How do I do?

1
  • I didn't think to ask this before, because I assumed your "cat_id" field is an integer of some variation. Now that I'm revisiting your question, I notice that you surrounded your cat_id values with quotations. If your cat_id is a string, then make sure you use the quotations in the example I gave. Commented Jul 29, 2011 at 15:45

4 Answers 4

5
SELECT * FROM cats WHERE cat_id IN (32, 33, 35, 37) ORDER BY sorting
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT *
FROM cats
WHERE cat_id IN ("32", "33", "34", "37")
ORDER BY sorting

Comments

0

Kristofer Hoch has the best answer however you should note you could also write

WHERE cat_id = 32 
      OR cat_id = 33
      OR cat_id = 35
      OR cat_id = 37

Comments

0

The examples from Conrad and Kristofer would work perfectly in the simple use case you've shown. Please be aware that using OR or IN will sometimes force the optimizer to create temporary tables. If you are writing a more complex query and the field used in the IN clause is not an index, be sure to run an EXPLAIN command.

EXPLAIN [you query here];

To identify this scenario. I've had to use UNION in some cases to optimize the query.

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.