简体   繁体   中英

Select records after ID

I have a MySQL table which contains the following two fields (There are others, but they're irrelevant)

creation_time | object_id  
1325384907    | 75b31b2c-78e9-46b9-afc8-6d90c0df2ee6
1325388538    | c87734c9-5109-4956-a1cb-7e719ebb4549
1325430911    | 79fe7f41-4a73-4aa3-b1e4-68be0253d9ab
[...]

What I would like to do is select from the table, ordered by time, all objects after a certain object_id

So the sort of thing I'd want is a query like

SELECT * FROM MYTABLE ORDER BY creation_time ASC WHERE object_id > 'c87734c9-5109-4956-a1cb-7e719ebb4549'

Which should return 1325430911 | 79fe7f41-4a73-4aa3-b1e4-68be0253d9ab 1325430911 | 79fe7f41-4a73-4aa3-b1e4-68be0253d9ab (Being the next record after that object ID).

This is for a paging system (IE 'get results after'), the only information we have is the uuid so we can't just select creation_time > x .

SELECT * FROM MYTABLE 
WHERE CREATION_TIME > (SELECT CREATION_TIME FROM MYTABLE WHERE OBJECTID='YOUR_OBJECT_ID_THAT_YOU_REALLY_KNOW' LIMIT 1) 
ORDER BY CREATION_TIME ASC

Please be aware of LIMIT 1 because if subquery returns more than 1 result, the query will be fail

SELECT Creation_Time, Object_ID
FROM `tableName`
WHERE Creation_Time > 
        (SELECT Creation_Time 
         FROM `tableName`
         WHERE Object_ID = 'idhere')
ORDER BY Creation_Time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM