Android/java Sqlite:如何检索最大日期时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13838107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:08:21  来源:igfitidea点击:

Android/java Sqlite: How to retrieve max datetime?

javaandroidsqlite

提问by Prateek

Below is the snapshot of what I have got as a query from sqlite db.

下面是我从 sqlite db 查询得到的快照。

After googling and reading a number of question around, I have come to know that to retrieve maximum datetime using aggregate functions like max()is not possible as sqlite doesn't support much datatypes but treats datatype as text.

在谷歌搜索并阅读了一些问题之后,我开始知道使用聚合函数检索最大日期时间max()是不可能的,因为 sqlite 不支持太多数据类型,但将数据类型视为text.

So, I have brought this data in a Listor at javalevel. So how could I now get the maximum datetime from this list.

所以,我把这些数据放在一个List或一个java级别。那么我现在如何从这个列表中获得最大日期时间。

Is there any direct construct for this format in java. Or do we have something at sqlite level that I coudn't find.

在java中是否有这种格式的直接构造?或者我们是否有一些我找不到的 sqlite 级别的东西。

Sqlite returned data

Sqlite 返回数据

回答by njzk2

texts can be compared, sorted and ordered in SQLite.

文本可以在 SQLite 中进行比较、排序和排序。

Your format appears to be YYYY-MM-dd hh:mm:ss. Lucky for you, ordering this format result in ordering by date.

您的格式似乎是 YYYY-MM-dd hh:mm:ss。幸运的是,订购此格式会导致按日期订购。

just

只是

select current_test_id from someTable order by test_datetime desc limit 1

or

或者

select current_test_id, max(test_datetime) from someTable

(or something, not entirely sure for the second one)

(或其他东西,不完全确定第二个)

回答by USKMobility

if you set the type of datetime field text then you can perform following query but datetime must be yyyy-mm-dd hh:mm:ss

如果您设置日期时间字段文本的类型,那么您可以执行以下查询,但日期时间必须是 yyyy-mm-dd hh:mm:ss

select max(datetime) from tableName;

回答by Samjin Mahadevan

To retrive max value from a set of Time of type (String) We have to do some concatenations using sub string .Using this Query max or min Time can be find out using sql lite

要从一组类型(字符串)的时间中检索最大值我们必须使用子字符串进行一些连接。使用此查询最大或最小时间可以使用 sql lite 找到

select max(datetime(case 
when substr(TimeIn,7,2)='PM' 
then substr(TimeIn,1,2)+12 
else substr(TimeIn,1,2) 
end  || ':' || substr(TimeIn,4,2) || ':' || '00'))  
from tablename 
where Date='10/06/2016'

回答by fedepaol

A common approach is to store the data converted as long.

一种常见的方法是将转换后的数据存储为长时间。

Use date.getTime()to get long from your Date instance and Date date = new Date(timestamp);to get a date object from your timestamp.

用于date.getTime()从 Date 实例中获取 long 并Date date = new Date(timestamp);从时间戳中获取日期对象。

Once you have a long in your db you can perform any ordering / comparison you want.

一旦你的数据库中有一个 long 你就可以执行任何你想要的排序/比较。