tags:

views:

115

answers:

5

Let's say I have a query like this-

SELECT * FROM table_name WHERE venue = "1" OR venue = "10"

That's great, works fine. But let's say I have a list of 50 venue numbers. Is there an easy way to do this where I don't have to write out 50 different ORs?

Thanks!

+3  A: 

You can use "IN" instead:

SELECT * FROM table_name WHERE venue IN (1, 12, 23, ... 150)
ChssPly76
Also keeping in mind that IN can also use a subselect.
R. Bemrose
A: 

Use the IN keyword: ... WHERE venue IN (2, 3, 5, 7, 11, ...) ;

Arthur Reutenauer
A: 
SELECT * FROM table_name WHERE venue IN ('1','10')

Best answer would be

SELECT * FROM table_name WHERE venue IN @myVar

Where @myVar is a list set in the query,

Zoidberg
You can't set IN parameters as prepared statement variable. You certainly can append `@myVar` to query text, but then I'm not sure why that would be "best".
ChssPly76
Well, if this was being called from an application, having a variable in the query that you can set is better than putting it inline, thats all I am trying to say. If your running the query in query analyser or SQLYog, then the way you wrote it would be just fine.
Zoidberg
A: 

Try...

SELECT * FROM table_name WHERE venue in ('1', '10')

I know it works in Oracle. "in" operator is probably part of standard SQL? Dunno.

edit: changed double to single quotes.

EMPraptor
A: 

Not sure if MySQL supports this...

Use the BETWEEN operator:

SELECT *
  FROM table
  WHERE venue BETWEEN '1' and '50';

Although you may need to be more explicit when dealing with character string datatypes:

SELECT *
  FROM table
  WHERE venue BETWEEN '1' and '9'
     OR venue BETWEEN '10' and 50';

Of course, BETWEEN won't help if your list of numbers are non-contiguous.

Loadmaster