开发者

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

开发者 https://www.devze.com 2022-12-11 04:38 出处:网络
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 t

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

精彩评论

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