简体   繁体   English

找到最大的时间价值

[英]Find the largest value of time

I am looking for the script which will search the largest time value from the mysql returned row. 我正在寻找将从mysql返回的行中搜索最大时间值的脚本。 Mysql returned row like: Mysql返回的行如下:

(123, 'new', datetime.datetime(2013, 1, 2, 16, 6, 21))
(122, 'old', datetime.datetime(2012, 12, 3, 18, 08, 36))
(125, 'new', datetime.datetime(2012, 12, 13, 20, 18, 16))

i tried with: 我尝试过:

value = 0
for row in rows:
  if row[2] > value:
    value = row
print value

if we print the row[2] value that will show like that 如果我们打印row [2]值,它将像这样显示

for row in rows:
 print row[2]

output: 2013-01-02 16:06:21
        2012-12-03 18:08:36
        2013-12-13 20:18:16

Anders Johansson answers your question neatly. 安德斯·约翰逊(Anders Johansson)巧妙地回答了您的问题。

However, you could also avoid the need to do this in Python to begin with, by simply requesting only that line of data. 但是,您也可以只通过请求那一行数据来避免在Python中开始这样做。 A SQL statement like: SQL语句如下:

SELECT DateTime FROM MyTable
ORDER BY CASE WHEN Status IS 'new' THEN 1 ELSE 0 END DESC, DateTime DESC LIMIT 1;

would achieve what you're trying to do, and give you only that one data point. 可以实现您想要做的事情,并且只给您一个数据点。

Update: 更新:

SELECT mydate FROM MyTable
WHERE type=4
ORDER BY FIELD(status, 'new', 'waited', 'old'), mydate DESC
LIMIT 1;

SQLFiddle SQLFiddle

您可以简单地使用:

print max(x[2] for x in rows)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM