我有一个Realty Model,用户可以在其中创建和更新其不动产。

这是一些数据。为了简单起见,我仅向您显示表格中的时间。日期在最上面。

Date: 2013-07-01

ID | created_at | updated_at
----------------------------
 1 | 13:00:00   | 14:00:00
 2 | 15:00:00   | 15:00:00
 3 | 16:00:00   | 17:00:00

Date: 2013-07-02

ID | created_at | updated_at
----------------------------
 4 | 10:00:00   | 10:00:00
 5 | 12:00:00   | 17:00:00
 6 | 18:00:00   | 18:00:00


在查看不动产时,我希望用户首先查看最新的不动产。因此,应将7月2日创建的房地产(ordered by date DESC)放在7月1日创建的房地产(4,5,6)之前。

现在是棘手的部分:

在每个特定的日子,我想区分在创建后已更新的房地产(1,2,3)和按时间对它们进行分类而仅创建而不更新的房地产(1,3,5)。

作为结论

我想首先拥有房地产。在此顺序下,我想先显示不更新2,4,6的不动产,然后显示不更新ordered by date DESC的不动产

结果将导致ordered by time DESC

提前致谢!

最佳答案

我认为您想要这样的order by

order by date(created_at) desc,
         (case when created_at = updated_at then created_at end) desc,
         updated_at desc

关于mysql - Rails时间戳:由updated_at/created_at组成的复杂顺序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17428225/

10-12 00:40
查看更多