本文介绍了ORDER BY id或date_created以显示最新结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表(实际上有几个),我想从最近的条目获得结果。这是我的 ORDER BY 子句选项:

I have a table (several actually) that I want to get results from with the most recent entries first. Here are my ORDER BY clause options:


  • date_created INT

  • id(当然是INT AUTO_INCREMENT)

其中插入了记录。我自然会像任何理智的人那样使用 date_created 字段,但我仍然对此感兴趣。

Both columns should equally represent the order in which the records were inserted. I naturally would use the date_created field like any sane person would, but I'm still curious about this.

我知道这可能是分裂的头发,但有什么原因或边缘情况下,为什么我不应该使用 id 列?

I know this is probably splitting hairs, but is there any reason or edge case why I should NOT use the id column?

EDIT:我认为这个问题对于我们想要真正表示插入顺序的值是含糊的。谢谢大家的所有答案,我将接受最好的一个,继续前进,因为我认为我已经通过假设ids总是顺序这个困难(见@ Wrikken的评论)。我的直觉本能是,开发者不应该考虑id,这是这里大多数的答案指向。

I'm thinking that this question is vague as to which value we want to truly represent the insert order. Thank you for all your answers everybody, I am going to accept the best one and move on because I think I have made this difficult by assuming that ids will always be in order (see @Wrikken's comment). My gut instinct is that id should never be considered by the developer, which is what most of the answers here are pointing to.

推荐答案

依靠ID列进行时间排序并不是一个好主意,因为这不是它的目的。基本上,ID只是该行的唯一键,没有更多。使用ID可能永远不会导致问题,但没有理由增加复杂性,假设ID的排序将始终保持。例如,您可能希望以后删除条目,然后手动插入新条目,或从其他来源导入过去带有时间戳的条目。如果您没有date_created列,则ID是您唯一的选择,但由于您有该列,请使用它,因为它是您的最佳选择。

It isn't a good idea to depend on the ID column for time ordering, because that isn't its purpose. Basically, the ID is just a unique key for that row, nothing more. Using ID might never cause problems, but there is no reason to add complexity of assuming that ordering by ID will always hold. For instance, you might in the future want to delete entries and then manually insert new entries, or import entries from some other source that are timestamped in the past. If you didn't have a date_created column, then ID would be your only option, but since you have the column, use it, as it is your best choice.

这篇关于ORDER BY id或date_created以显示最新结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 12:17