Question

I am using python and MySQLdb module. I have a table that looks like this;

TableA
------
id
latest_time_seen

The format of latest_time_seen is 2014-02-05 00:00:50

From TableA, I would like to create a view on mysql that is similar to TableA and the only difference is that it contains an extra calculated field called secs_diff.

secs_diff is equal to the number of seconds difference between current time and latest_time_seen.

The output table will look something like this;

OutputTable
-----------
id
latest_time_seen
secs_diff

I doubt if I have problems writing a python function to calculate secs_diff. But I am stuck at how to integrate this function into a MySQL query that generates a view that looks like OutputTable.

How can such a view be generated using python and MySQLdb module? Or would it be better to use a temporary table? It will be better if such a view can be generated purely from a MySQL query. But I am not sure if MySQL query is flexible enough to calculate the time difference.

Thank you very much.

Était-ce utile?

La solution

yes you can do with pure MySQL,

try using this query

select *,TIME_TO_SEC(TIMEDIFF(NOW(), latest_time_seen)) as secs_diff from tbl

see DEMO

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top