开发者

How to search for a value being in a known range in mySQL

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 wri开发者_高级运维te out 50 different ORs?

Thanks!


You can use "IN" instead:

SELECT * FROM table_name WHERE venue IN (1, 12, 23, ... 150)


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,


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜