Surya Gupta
Surya Gupta

Reputation: 117

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 returned row like:

(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

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

Upvotes: 1

Views: 112

Answers (2)

jdotjdot
jdotjdot

Reputation: 17042

Anders Johansson answers your question neatly.

However, you could also avoid the need to do this in Python to begin with, by simply requesting only that line of data. A SQL statement like:

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

Upvotes: 1

Anders Johansson
Anders Johansson

Reputation: 4006

You can simply use:

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

Upvotes: 1

Related Questions