Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Selecting all BUT the last row in a table?

Tags:

sql

How can I select all BUT the last row in a table?

I know I can select the last via SELECT item FROM table ORDER BY id DESC LIMIT 1 but I want to select ALL but the last.

How can I do this?

Selecting a certain amount and using ASC won't work because I won't know how many rows there are.

I thought about doing a row count and taking that amount and selecting that -1 and using ORDER BY id ASC but are there any easier ways?

Thanks.

like image 321
user2888263 Avatar asked Oct 19 '13 19:10

user2888263


1 Answers

If Id is unique, you can do it as follows:

SELECT ...
FROM MyTable
WHERE Id < (SELECT MAX(Id) FROM MyTable)
like image 53
Joe Avatar answered Oct 05 '22 19:10

Joe