How to SELECT the last 10 rows of an SQL table whi

2020-02-09 07:22发布

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

13条回答
一夜七次
2楼-- · 2020-02-09 07:33

If you want to retrieve last 10 records from sql use LIMIT. Suppose the data base contains 20 records.Use the below query

SELECT * FROM TABLE_NAME LIMIT 10,20;

where 10,20 is the offset value.Where 10 represent starting limit and 20 is the ending limit.

i.e 20 -10=10 records

查看更多
女痞
3楼-- · 2020-02-09 07:35

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
查看更多
够拽才男人
4楼-- · 2020-02-09 07:37

That can be done using the limit function, this might not seem new but i have added something.The code should go:

SELECT * FROM table_name LIMIT 100,10;

for the above case assume that you have 110 rows from the table and you want to select the last ten, 100 is the row you want to start to print(if you are to print), and ten shows how many rows you want to pick from the table. For a more precised way you can start by selecting all the rows you want to print out and then you grab the last row id if you have an id column(i recommend you put one) then subtract ten from the last id number and that will be where you want to start, this will make your program to function autonomously and for any number of rows, but if you write the value directly i think you will have to change the code every time data is inserted into your table.I think this helps.Pax et Bonum.

查看更多
欢心
5楼-- · 2020-02-09 07:48
SELECT * FROM big_table ORDER BY A DESC LIMIT 10
查看更多
等我变得足够好
6楼-- · 2020-02-09 07:48

If you're doing a LOAD DATA INFILE 'myfile.csv' operation, the easiest way to see if all the lines went in is to check show warnings(); If you load the data into an empty or temporary table, you can also check the number of rows that it has after the insert.

查看更多
7楼-- · 2020-02-09 07:48

If you have not tried the following command

SELECT TOP 10 * FROM big_table ORDER BY id DESC;

I see it's working when I execute the command

SELECT TOP 10 * FROM Customers ORDER BY CustomerId DESC;

in the Try it yourself command window of https://www.w3schools.com/sql/sql_func_last.asp

查看更多
登录 后发表回答