Lasse A Karlsen Lasse A Karlsen - 4 months ago 10
MySQL Question

MySQL: How to select all rows from a table EXCEPT the last one

I have a table with N rows, and I wanna select N-1 rows.

Suggestions on how to do this in one query, if it's possible..?

Answer

Does the last row have the highest ID? If so, I think this would work:

SELECT * FROM TABLE WHERE ID != (SELECT MAX(ID) FROM TABLE)

MySQL does allow subselects in the current version, right?

However, in most cases, it'd probably perform better if you selected all the rows and then filtered the unwanted data out in your application.