Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to SELECT the last 10 rows of an SQL table which has no ID field?

I have an MySQL table with 25000 rows.

This is an imported CSV file so I want to look at the last ten rows to make sure it imported everything.

However, since there is no ID column, I can't say:

SELECT * FROM big_table ORDER BY id DESC

What SQL statement would show me the last 10 rows of this table?

The structure of the table is simply this:

columns are: A, B, C, D, ..., AA, AB, AC, ... (like Excel)
all fields are of type TEXT
like image 463
Edward Tanguay Avatar asked Sep 06 '25 03:09

Edward Tanguay


2 Answers

All the answers here are better, but just in case... There is a way of getting 10 last added records. (though this is quite unreliable :) ) still you can do something like

SELECT * FROM table LIMIT 10 OFFSET N-10
  • N - should be the total amount of rows in the table (SELECT count(*) FROM table). You can put it in a single query using prepared queries but I'll not get into that.
like image 153
Ivan Avatar answered Sep 07 '25 20:09

Ivan


Select from the table, use the ORDER BY __ DESC to sort in reverse order, then limit your results to 10.

SELECT * FROM big_table ORDER BY A DESC LIMIT 10
like image 43
Steven Scott Avatar answered Sep 07 '25 22:09

Steven Scott