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

后端 未结 13 1391
灰色年华
灰色年华 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 18:10

    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.

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

    SQL tables have no implicit ordering, the order has to come from the data. Perhaps you should add a field to your table (e.g. an int counter) and re-import the data.

    However that will only give the order of the import and not the data. If your data has no ordering you have to find out how to add it.

    EDIT: you say

    ...to make sure it imported everything.

    What's wrong with using row count?

    0 讨论(0)
  • 2020-12-15 18:13
    SELECT * FROM big_table ORDER BY A DESC LIMIT 10
    
    0 讨论(0)
  • 2020-12-15 18:13

    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.

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

    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

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

    executing a count(*) query on big data is expensive. i think using "SELECT * FROM table ORDER BY id DESC LIMIT n" where n is your number of rows per page is better and lighter

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