Rails - 按小时排序

发布于 2024-11-04 13:45:21 字数 189 浏览 0 评论 0原文

@events = Event.all(:order => "date DESC")

按日期对我的事件进行排序,使用 datetime 列。

是否可以不按日期而仅按小时排序? (我正在起诉 sqlite3 数据库)

谢谢!

@events = Event.all(:order => "date DESC")

to order my events by date, a datetime column.

Is it possible to order them not by date, but only by hour? (I'm suing sqlite3 database)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫七 2024-11-11 13:45:21

对于 SQLite,

@events = Event.all(:order => "time(date) DESC")

请小心使用,因为它会忽略日期。 (请参阅下面的“稍后”。)

CREATE TABLE test (date datetime primary key);
INSERT INTO "test" VALUES('2011-01-01 08:00:00');
INSERT INTO "test" VALUES('2011-01-01 08:13:00');
INSERT INTO "test" VALUES('2011-01-01 09:23:00');
INSERT INTO "test" VALUES('2011-01-02 09:15:00');

只有一个日期是 1 月 2 日。

sqlite> select * from test order by time(date) desc;
2011-01-01 09:23:00
2011-01-02 09:15:00
2011-01-01 08:13:00
2011-01-01 08:00:00

稍后。 。 .

我意识到您想按小时排序,而不是按时间排序。这个有问题的要求有不同的表达方式,并且排序也不同。

@events = Event.all(:order => "strftime('%H', date) DESC")

sqlite> select date from test order by strftime('%H', date) desc;
2011-01-01 09:23:00
2011-01-02 09:15:00
2011-01-01 08:00:00
2011-01-01 08:13:00

最后两行按小时正确排序,按时间错误排序。

再晚一点。 。 .

OP部署在Heroku上,不支持SQLite。要按小时降序排序,OP 可能需要类似

@events = Event.all(:order => "extract (hour from date) DESC")

“停止使用一个平台进行开发和使用不同的平台进行部署”之类的内容。

For SQLite,

@events = Event.all(:order => "time(date) DESC")

Use that with care, because it ignores the date. (And see "Moments later" below.)

CREATE TABLE test (date datetime primary key);
INSERT INTO "test" VALUES('2011-01-01 08:00:00');
INSERT INTO "test" VALUES('2011-01-01 08:13:00');
INSERT INTO "test" VALUES('2011-01-01 09:23:00');
INSERT INTO "test" VALUES('2011-01-02 09:15:00');

Only one of the dates is on Jan 2.

sqlite> select * from test order by time(date) desc;
2011-01-01 09:23:00
2011-01-02 09:15:00
2011-01-01 08:13:00
2011-01-01 08:00:00

Moments later . . .

I realized you wanted to sort by hour, not by time. That questionable requirement takes a different expression, and sorts differently.

@events = Event.all(:order => "strftime('%H', date) DESC")

sqlite> select date from test order by strftime('%H', date) desc;
2011-01-01 09:23:00
2011-01-02 09:15:00
2011-01-01 08:00:00
2011-01-01 08:13:00

The last two rows are sorted correctly by hour, incorrectly by time.

Still later . . .

The OP deploys on Heroku, which doesn't support SQLite. To sort by hour descending, the OP probably needs something like

@events = Event.all(:order => "extract (hour from date) DESC")

And stop using one platform for development and a different platform for deployment.

锦上情书 2024-11-11 13:45:21

由于 Heroku 使用 PostgreSQL,因此您可以使用:

@events = Event.all(:order => "date_part(hour, date) DESC")

如果您在本地使用 SQLite3 并使用 PostgreSQL 进行部署,那么您将在开发时遇到问题 - SQL 在平台之间不一致。

Since Heroku uses PostgreSQL, you can use:

@events = Event.all(:order => "date_part(hour, date) DESC")

You'll have problems developing if you use SQLite3 locally and PostgreSQL for deployment - SQL is not consistent between platforms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文