Hello.. Is it possible to execute a select query where the id column is within the last 50 rows? So like WHERE id > total id - 50.
Something like that? Not sure of the syntax! Cheers
Hello.. Is it possible to execute a select query where the id column is within the last 50 rows? So like WHERE id > total id - 50.
Something like that? Not sure of the syntax! Cheers
You can just combine ORDER BY with LIMIT to achieve this. You can read about them in the docs on SELECT.
Did you mean just select the last 50 rows, or select something in the last 50 rows? This is how you would select from the last 50:
SELECT *
FROM table
WHERE id IN ( SELECT id FROM table ORDER BY id DESC LIMIT 50 )
AND whatImLookingFor = 'Something'
EDIT: Or in databases that use the other syntax:
SELECT *
FROM table
WHERE id IN ( SELECT TOP 50 id FROM table ORDER BY id DESC )
AND whatImLookingFor = 'Something'
You can use the above if you just want the last 50 items, and it's probably the cleanest and best way. But if you really care about the ID for some reason, you can use the MAX function:
SELECT *
FROM table
WHERE id > (SELECT MAX( id ) FROM table) - 50
AND whatImLookingFor = 'Somesuch'
This of course isn't terribly efficient and is somewhat unweidly, but will do exactly what you were asking about.