帮助在 SQLite 中按日期对结果进行排序
SQLite 有什么方法可以按日期排序,并使结果按时间而不是按字母顺序排序吗?
例如:
SELECT * FROM details GROUP BY date;
John | Smith | April 01, 2011
John | Smith | April 03, 2011
John | Smith | April 04, 2011
John | Smith | March 25, 2011
三月应该在四月之前。
我猜测这里的答案是将我的日期存储为长时间戳,但是我不确定是否可以使用 SQLite 更轻松地完成此操作。
谢谢!
Is there any way in SQLite to ORDER BY a date, and have result be ordered by time rather than alphabetically?
For example:
SELECT * FROM details GROUP BY date;
John | Smith | April 01, 2011
John | Smith | April 03, 2011
John | Smith | April 04, 2011
John | Smith | March 25, 2011
March should come before April.
I'm guessing that the answer here is to store my dates as long timestamps, however I wasn't sure if it could be done more easily with SQLite.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SQLite 中没有内置的
DATE
类型(如其他一些数据库管理系统中存在的那样),但它确实对日期和时间函数有一个很好的补充:http://www.sqlite.org/lang_datefunc.html您可以使用 date("April 01, 2011") 来返回一个ISO-8601 日期(例如,2011-04-01)。
这种格式具有可读字符串和可排序的优点。根据标准字符串比较规则,2011-03-25 自然位于 2011-04-01 之前,因此不需要特殊操作。
因此,以该格式存储日期,并使用
date()
函数(或其他相关函数)获取该格式。There isn't a built-in
DATE
type in SQLite (as exists in some other database management systems), but it does have a nice complement of date and time functions: http://www.sqlite.org/lang_datefunc.htmlYou can use date("April 01, 2011") to get back an ISO-8601 date (e.g., 2011-04-01).
This format has the advantages of both being a readable string and being sortable. 2011-03-25 naturally comes before 2011-04-01 by standard string comparison rules, so there's no special operation required.
So, store your dates in that format, and get that format using the
date()
function (or other relevant function).您可以将日期转换为实际日期以便进行比较。
尝试将
order by julianday(date)
添加到查询末尾http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions
you can convert date to an actual date in order to compare it.
try to add
order by julianday(date)
to the end of the queryhttp://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions
日期必须是字符串而不是实际日期。您需要将日期转换为实际日期。这是 Sqlite 日期和时间函数的链接。
http://www.sqlite.org/lang_datefunc.html
date must be a string and not an actual date. You would need to convert date to an actual date. Here is a link to Sqlite date and time functions.
http://www.sqlite.org/lang_datefunc.html
你可以做这样的事情
希望这有帮助
You can do something like this
Hope this helps