Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL select IN range

Tags:

mysql

Is it possible to define a range for the IN part of the query, something like this

SELECT job FROM mytable WHERE id IN (10..15); 

Instead of

SELECT job FROM mytable WHERE id IN (10,11,12,13,14,15); 
like image 317
Owen Avatar asked May 14 '12 15:05

Owen


People also ask

How do I select a range in MySQL?

IN(start,end): It means that the intermediate value between start and end won't get displayed. For the above logic, you can use BETWEEN. BETWEEN clause is inclusive, for example, suppose there are 1,2,3,4,5,6 numbers.

How do I find the range of a row in MySQL?

You have to use the LIMIT clause in the SELECT query. MySQL allows you to set two parameters for the clause, the offset (first parameter) and the number of rows to fetch (second parameter). SELECT * FROM `ABC` LIMIT 0, 100 SELECT * FROM `ABC` LIMIT 100, 100 SELECT * FROM `ABC` LIMIT 200, 100 -- etc...

How do I select between values in MySQL?

The MySQL BETWEEN OperatorThe BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included.


2 Answers

You can't, but you can use BETWEEN

SELECT job FROM mytable WHERE id BETWEEN 10 AND 15 

Note that BETWEEN is inclusive, and will include items with both id 10 and 15.

If you do not want inclusion, you'll have to fall back to using the > and < operators.

SELECT job FROM mytable WHERE id > 10 AND id < 15 
like image 92
ESG Avatar answered Nov 09 '22 04:11

ESG


To select data in numerical range you can use BETWEEN which is inclusive.

SELECT JOB FROM MYTABLE WHERE ID BETWEEN 10 AND 15; 
like image 32
Lalit Dashora Avatar answered Nov 09 '22 03:11

Lalit Dashora