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?
- 
        What have you tried so far?alexherm– alexherm2019-07-26 21:37:05 +00:00Commented 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?daShier– daShier2019-07-26 21:49:11 +00:00Commented 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?Gordon Linoff– Gordon Linoff2019-07-27 12:02:43 +00:00Commented 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 keyRubicelia Patlán– Rubicelia Patlán2019-07-29 19:48:28 +00:00Commented Jul 29, 2019 at 19:48
                    
                        Add a comment
                    
                 | 
            
                
            
        
         
    2 Answers
I think you want it like this?
SELECT *
FROM table
WHERE serialnumber<=@yourserialnumberwhichyousearch
ORDER BY serialnumber DESC
LIMIT 51;
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.
