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

后端 未结 13 1390
灰色年华
灰色年华 2020-12-15 17:38

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, sinc

相关标签:
13条回答
  • 2020-12-15 17:50

    You can use the "ORDER BY DESC" option, then put it back in the original order:

    (SELECT * FROM tablename ORDER BY id DESC LIMIT 10) ORDER BY id;

    0 讨论(0)
  • 2020-12-15 18:01

    A low-tech approach: Doing this with SQL might be overkill. According to your question you just need to do a one-time verification of the import.

    Why not just do: SELECT * FROM ImportTable

    and then scroll to the bottom of the results grid and visually verify the "last" few lines.

    0 讨论(0)
  • 2020-12-15 18:05

    All the answers here are better, but just in case... There is a way of getting 10 last added records. (thou 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.

    0 讨论(0)
  • 2020-12-15 18:08

    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
    
    0 讨论(0)
  • 2020-12-15 18:08

    you can with code select 10 row from end of table. select * from (SELECT * FROM table1 order by id desc LIMIT 10) as table2 order by id"

    0 讨论(0)
  • 2020-12-15 18:09

    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

    0 讨论(0)
提交回复
热议问题