简体   繁体   English

如何从两个单独的列中减去时间戳,然后将此数据输入表中

[英]How to subtract timestamps from two seperate columns, then enter this data into the table

I am attempting to create a sign-in, sign-out program using python and mysql on a raspberry pi. 我正在尝试在树莓派上使用python和mysql创建登录,注销程序。 I have so far successfully created a system for registering, signing in, and signing out, but am having trouble with the final component. 到目前为止,我已经成功创建了一个用于注册,登录和注销的系统,但是在最后一个组件方面遇到了麻烦。 I am trying to subtract the two separate time-stamps (signin, signout) using TIMESTAMPDIFF, and then entering this value into a separate column in the table. 我试图使用TIMESTAMPDIFF减去两个单独的时间戳(登录,登出),然后将此值输入到表中的单独列中。 However, the statement I am using is not working. 但是,我正在使用的语句不起作用。

Code: 码:

employee_id = raw_input("Please enter your ID number")

minutes_calc = "INSERT INTO attendance (minutes) WHERE id = %s VALUES (TIMESTAMPDIFF(MINUTE, signin, signout))"

try:

    curs.execute(minutes_calc, (employee_id))
    db.commit()
except:

    db.rollback()
    print "Error"

Table Structure so far (apologies for formatting): 到目前为止的表结构(格式化的道歉):

name    |    id    |    signin           |    signout          |    minutes    |

Dr_Kerbal      123     2016-08-21 22:57:25   2016-08-21 22:59:58    NULL

Please Note that I am in need of a statement that can subtract the timestamps regardless of their value instead of a statement that focuses on the specific timestamps in the example. 请注意,我需要一个示例,该示例可以不考虑时间戳的值而减去时间戳,而不是在示例中着重于特定时间戳的语句。

Additionally the datatype for the column minutes is decimal (10,0) as I initially intended for it to be in seconds (that was when I encountered other issues which have been solved since), its null status is set to YES, and the default value is NULL. 另外,分钟数列的数据类型为十进制(10,0),因为我最初希望以秒为单位(也就是说,当我遇到自此以来已解决的其他问题时),其空状态设置为YES,并且默认为值为NULL。

Thank You for your Assistance 谢谢您的帮助

You need an update query not INSERT 您需要一个update查询而不是INSERT

UPDATE attendance 
SET `minutes` = TIMESTAMPDIFF(MINUTE, signin, signout);

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

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