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..?
Another technique I don't see listed here is
SELECT * FROM table ORDER BY id DESC LIMIT 10000 OFFSET 1;
This will give you the records ordered by id descendant except first, that is except the last in the original order.
Note that with this method you will only take 10000 records, however this number can be as high as you want but cannot be omitted.
The advantage of this method is that you can order by whatever you want.
The disadvantage is that it gives you the records ordered from last to first.
Finally it worths noting that the other methods here works quite well
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With