热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何选择没有ID字段的SQL表的最后10行?-HowtoSELECTthelast10rowsofanSQLtablewhichhasnoIDfield?

IhaveanMySQLtablewith25000rows.我有一个MySQL表,有25000行。ThisisanimportedCSVfilesoIwantt

I have an MySQL table with 25000 rows.

我有一个MySQL表,有25000行。

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

这是一个导入的CSV文件,所以我想查看最后10行以确保它导入了所有内容。

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

但是,由于没有ID列,我不能说:

SELECT * FROM big_table ORDER BY id DESC

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

什么SQL语句会显示该表的最后10行?

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 个解决方案

#1


11  

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.

SQL表没有隐式排序,顺序必须来自数据。也许您应该向表中添加一个字段(例如int计数器)并重新导入数据。

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?

使用行计数有什么问题?

#2


17  

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

这里所有的答案都更好,但只是以防万一……有一种方法可以获得最后添加的10条记录。(你这是相当不可靠的)你还是可以做些类似的事

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.

N -应该是表中的行总数(从表中选择count(*)))。您可以使用预先准备好的查询将它放在一个查询中,但我不会深入讨论这个问题。

#3


15  

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

从表中选择,使用de DESC的顺序按逆向排序,然后将结果限制为10。

SELECT * FROM big_table ORDER BY A DESC LIMIT 10

#4


4  

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.

如果在myfile中执行加载数据。csv的操作,查看所有行是否都输入的最简单的方法是检查显示警告();如果将数据加载到一个空表或临时表中,还可以检查插入之后的行数。

#5


4  

SELECT * FROM big_table ORDER BY A DESC LIMIT 10

#6


4  

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

你可以使用“由DESC订购”选项,然后把它放回原来的顺序:

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

(根据id DESC LIMIT从表名订单中选择*)按id选择订单;

#7


2  

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

在大数据上执行count(*)查询是非常昂贵的。我认为使用“SELECT * FROM table ORDER BY id DESC LIMIT n”,其中n是每页的行数,这样更好更轻

#8


1  

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.

一种低技术含量的方法:使用SQL来实现这一点可能有些过分。根据您的问题,您只需要对导入进行一次验证。

Why not just do: SELECT * FROM ImportTable

为什么不直接做:从ImportTable中选择* ?

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

然后滚动到结果网格的底部,视觉上验证“最后”几行。

#9


0  

If you know how many rows to expect, I would create a separate temporary table in your database of the expected structure, append into that, then check the count... Once you are good with that, then you can massage that data before appending it into your final production table.

如果您知道要期望多少行,我将在您的预期结构的数据库中创建一个单独的临时表,并将其添加到其中,然后检查计数…一旦您很好地使用了这些数据,那么您就可以在将数据添加到最终的生产表之前对其进行处理。

#10


0  

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"

您可以使用代码从表的末尾选择10行。选择*(从表1中选择*按id desc限制10)作为表2按id排序"

#11


-1  

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

如果您想从sql中检索最后10条记录,请使用LIMIT。假设数据库包含20条记录。使用以下查询

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.

其中10 20是偏移值。其中10代表起始极限,20代表结束极限。

i.e 20 -10=10 records

我。e 20 -10 = 10记录

#12


-1  

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.

对于上述情况,假设您从表中有110行,并且您想要选择最后10行,100是您想要开始打印的行(如果要打印的话),10表示您想从表中选择多少行。更精密的你可以通过选择所有的行你想打印出来然后你抓住最后一行id如果你有一个id列(我建议你把一个),那么减去十最后一个id号,你想从哪里开始,这将使你的程序自动函数和任意数量的行,但是如果你写的价值直接我认为你需要改变代码每次数据插入到表中。我认为这可以帮助。罗马帝国等词。

#13


-1  

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

在https://www.w3schools.com/sql/sql_func_last.asp的Try it yourself命令窗口中


推荐阅读
  • 1.数据准备#测试数组vectorc(5,34,65,36,67,3,6,43,69,59,25,785,10,11,14)vector##[1]53465366736436959 ... [详细]
  • 两种方式实现Flink异步IO查询Mysql
    如官网所描述的Flink支持两种方式实现异步IO查询外部系统http ... [详细]
  • 1.方法一:采用OleDB读取EXCEL文件:把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下:publicDa ... [详细]
  • 结束jquery时间不长,写代码感觉很生,而且敢接写起来很费劲做点简单的总结。首先主要要先引入.juery.js文件第二一些js插件文件也要在jquer ... [详细]
  • 安全等于运算符()这个操作符和操作符执行相同的比较操作,不过可以用来判断NULL值。在两个操作数均为NULL时,其返回值为1而不为NULL;而当一个操作数为NULL时,其返回 ... [详细]
  • 在网上看到有Bootstrap2的Modaldialog垂直居中问题解决方法,这种方法自己试了一下,并不能完全居中,并且窗口的大小不一样的话,每次显示的margin-top值也会改 ... [详细]
  • R语言基础_数据导入&保存
    数据分析文件常用的储存格式为CSV(.csv)和EXCEL(.xlsx),其余文 ... [详细]
  • POI编程
    POI编程1简介在我们实际的开发中,表现层的解决方案虽然有多样,但是IE浏览器已成为最多人使用的浏览器,因为大家都用Windows。在企业办公系统中 ... [详细]
  • 闲话少说,直接切入主题,之前也是用一下其他的IDE,但是总是在vi和IDE之间来回切换,比较麻烦,于是乎,找了几个插件亲身体验,功能挺不错;1、安装vi7.0vim的官方网站是:www.vmuni ... [详细]
  • MySQL重大Bug!自增主键竟然不是连续递增?
    MySQL重大Bug!自增主键竟然不是连续递增? ... [详细]
  • git-canal:错误修改
    问题:2016-05-0422:53:48.848[destinationexample,address127.0.0.1:3306,EventParser]ERRO ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
author-avatar
七月de七月小
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有