我有一个mysql查询,它从数据库中提取创建和修改时间,然后将它们更改为unix时间
SELECT
tn,
title,
UNIX_TIMESTAMP(create_time) AS create_time,
UNIX_TIMESTAMP(change_time) AS change_time
FROM
ticket
WHERE
responsible_user_id = 2
AND ticket_state_id IN (2, 3, 10)
ORDER BY
change_time DESC
结果如下:
+---------+----------------------+-------------+-------------+
| tn | title | create_time | change_time |
+---------+----------------------+-------------+-------------+
| 7409025 | Re: otrs | 1459861204 | 1459861263 |
| 7408967 | RINAS@cartrack.co.za | 1459765215 | 1459836797 |
| 7408935 | FW: Vod | 1459767210 | 1459836797 |
如何从create_time行中减去change_time行,以便行在名为ave的新行中显示结果。
+---------+----------------------+-------------+-------------+------+
| tn | title | create_time | change_time | ave |
+---------+----------------------+-------------+-------------+------+
| 7409025 | Re: otrs | 1459861204 | 1459861263 | 59 |
| 7408967 | RINAS@cartrack.co.za | 1459765215 | 1459836797 | 71582|
| 7408935 | FW: Vod | 1459767210 | 1459836797 | 69587|
答案 0 :(得分:1)
UNIX_TIMESTAMP(change_time) - UNIX_TIMESTAMP(create_time) as ave
您可以使用数学函数和算术运算符来计算条目。
http://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html
答案 1 :(得分:1)
由于您正在使用时间戳,因此您应该使用UNIX_TIMESTAMP
函数。这是一个例子
SELECT UNIX_TIMESTAMP(create_time) - UNIX_TIMESTAMP(change_time) as output
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
您的最终查询看起来像这样
SELECT
tn,
title,
UNIX_TIMESTAMP(create_time) AS create_time,
UNIX_TIMESTAMP(change_time) AS change_time,
UNIX_TIMESTAMP(create_time) - UNIX_TIMESTAMP(change_time) AS time_diff
FROM
ticket
WHERE
responsible_user_id = 2
AND ticket_state_id IN (2, 3, 10)
ORDER BY
change_time DESC
答案 2 :(得分:1)
你几乎做到了。
SELECT
tn,
title,
UNIX_TIMESTAMP(create_time) AS create_time,
UNIX_TIMESTAMP(change_time) AS change_time,
(UNIX_TIMESTAMP(change_time) - UNIX_TIMESTAMP(create_time)) AS ave
FROM
ticket
WHERE
responsible_user_id = 2
AND ticket_state_id IN (2, 3, 10)
ORDER BY
change_time DESC
答案 3 :(得分:0)
假设在for循环中你创建时间为$ createTime [$ i]并将时间更改为$ changeTime [$ i];然后在回显单行中的值后,只需减去这两个变量,该值将为存储在列ave中的特定行中。