Rubicelia Patlán
Rubicelia Patlán

Reputation: 15

MySQL query to select specific data range

I have a huge database in MySQL, one of the columns contains a serial number (in my table i have about 17,000 serial numbers), i want to select a specific serial number and to display all the columns of this number and of the 50 serial numbers before it, how can i do this?

Upvotes: 0

Views: 142

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1271231

You don't state that the serial number is unique. If not, then a join is one method:

select t.*
from t join
     (select t.*
      from t
      where t.serialnumber < @sn
      order by t.serialnumber desc
      limit 50
     ) t50
     on t.serialnumber = @sn or t.serialnumber = t50

Note that this will return 50 serial numbers if your given number is not in the data.

Upvotes: 0

lewa9
lewa9

Reputation: 41

I think you want it like this?

SELECT *
FROM table
WHERE serialnumber<=@yourserialnumberwhichyousearch
ORDER BY serialnumber DESC
LIMIT 51;

Upvotes: 2

Related Questions