0

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?

4
  • What have you tried so far? Commented Jul 26, 2019 at 21:37
  • Are the serial numbers sequential, or are some numbers skipped? Can you show the table schema? Is the serial number the primary key for the table? Commented Jul 26, 2019 at 21:49
  • Is the serial number unique in the table? Are you looking for 51 rows or for all the rows associated with 51 serial numbers? Commented Jul 27, 2019 at 12:02
  • Some numbers are skipped, the primary key is different for every serial number, i have two data from the same serial number but with different primary key Commented Jul 29, 2019 at 19:48

2 Answers 2

2

I think you want it like this?

SELECT *
FROM table
WHERE serialnumber<=@yourserialnumberwhichyousearch
ORDER BY serialnumber DESC
LIMIT 51;
Sign up to request clarification or add additional context in comments.

2 Comments

This only works in POSTGRES as far as I know, MYSQL is LIMIT 0,50
Thanks! I think now its right. Could you check the query again. It should contain 51 Elements, because this specific element is asked (1 Element) and 50 Elements before.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.