从最后一行开始搜索mysql表 [英] Search mysql table starting from last row

查看:101
本文介绍了从最后一行开始搜索mysql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我知道在关系数据库上没有最后一行"之类的东西,但是我找不到更好的词来解释我想要做什么.我有一个如下表所示,其中idAUTO_INCREMENT字段,而time是当前的Unix时间戳.请注意,我可以为不同的行使用相同的时间戳.

So, I know that there is no such thing as "last row" on relational databases but I couldn't find a better word to explain what I want to do. I have a table that looks like the following, where id is an AUTO_INCREMENT field and time is the current Unix Time Stamp. Note that I can have the same time stamp for different rows.

    ╔════╦════════════╦════════════╦═════════════╦════════════╗
    ║ id ║  field_1   ║   field_2  ║   field_n   ║    time    ║
    ╠════╬════════════╬════════════╬═════════════╬════════════╣
    ║  1 ║ data_field ║ data_field ║ data_field  ║ 1369748934 ║
    ║  2 ║ data_field ║ data_field ║ data_field  ║ 1369748935 ║
    ║  3 ║ data_field ║ data_field ║ data_field  ║ 1369748936 ║
    ║  4 ║ data_field ║ data_field ║ data_field  ║ 1369748936 ║
    ║  5 ║ data_field ║ data_field ║ data_field  ║ 1369748938 ║
    ║  6 ║ data_field ║ data_field ║ data_field  ║ 1369748939 ║
    ╚════╩════════════╩════════════╩═════════════╩════════════╝

我想要做的是从最高的time字段开始搜索的选择,并且由于我知道它是从最低到最高的排序,所以我希望当第一行不符合我的WHERE子句时停止搜索因为其他所有行也不会匹配.我认为MySQL所做的是在所有行中进行搜索,但是由于我知道我的数据结构并且知道我想要的数据位置,因此不需要在整个表中进行搜索.同样,如果我使用ORDER BY,我相信WHERE子句的所有内容都将首先执行,因此我要做的基本上是搜索整个行集,然后对其进行排序.重要的一件事是我不知道要使用LIMIT子句将检索多少行.

What I want to do is a select that starts searching from the highest time field and since I know it is sorted from lowest to highest I want it to stop searching when the first row does not mach my WHERE clause because all the other rows won't match it as well. What I think MySQL does is search through all the rows but since I know my data structure and I know the location of the data I want I don't need to search through the entire table. Also if I use ORDER BY, I believe that all the contents of my WHERE clause will be executed first, so what I will be doing is basically search the entire row set and then order it. One important thing is I don't know how many rows will be retrieved in order to use a LIMIT clause.

这是一个示例查询:

SELECT fields FROM table WHERE time > timestamp

所以我想获得从给定日期到现在的所有结果.

So I want to get all the results starting from a given date up to now.

非常感谢您的帮助.

推荐答案

我想做的是从最高时间字段开始搜索的选择,由于我知道它是从最低到最高排序的,所以我希望它在...出现时停止搜索.

What I want to do is a select that starts searching from the highest time field and since I know it is sorted from lowest to highest I want it to stop searching when ...

我真的不认为您可以告诉MySQL在满足特定条件后停止查询.

I really don't think you can tell MySQL to stop querying after a certain condition is met.

事实上,我也不认为你也应该在乎.查询是数据库的工作,它做得很好.如果是性能问题,那么几个世纪后您才应该看到任何不同:)

As a matter of fact, I really don't think you should care either. Querying is the database job and it does it pretty well. If it's a matter of performance, you should not see any difference until many centuries :)

这篇关于从最后一行开始搜索mysql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆