Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order by date and select until specific id is reached

Tags:

mysql

I have a simple table with ID, STATUS, DATE columns, the rows in the table are ordered by DATE, I want to get all the rows until a specific ID is reached, and then stop, something like:

SELECT FROM myTable WHERE `DATE` <= '2017-10-09' ORDER BY `DATE` ASC UNTIL? `ID` = 119;

enter image description here

I like to know if that is possible somehow, to stop on a specific ID, whatever the ID was..

Thanks.

EDIT EXPLAINING

I want to select rows that are ordered under any column, but stop when a specific provided ID is reached. in the above image the result should be all the rows except the ones below the row 119.

I hope it's clear now.

like image 528
tinyCoder Avatar asked Jan 19 '26 14:01

tinyCoder


1 Answers

Something like this might work:

SET @marker = NULL;
SELECT *
FROM myTable
WHERE `DATE` <= '2017-10-09'
    AND ISNULL(@marker := IF(id = 119, 1, @marker))
ORDER BY `DATE` ASC;
like image 104
Cobra_Fast Avatar answered Jan 22 '26 04:01

Cobra_Fast