table:message [id,username,userid,message,type,timediff,timestamp]
我有一个名为message的表。消息启动后,将创建一个新行,其时间戳和timediff为null。
消息结束后,我想将其插入时差不同的行中。
例如开始1,username,1234,message,start/s,null,000001
结束:2,username,1234,message,end/s,**20**,000021
INSERT into Messages.message (username,userid,message,type,timediff)
values ("username","1234","Test","Ended/S",(
select sum(unix_timestamp(CURRENT_TIMESTAMP())-unix_timestamp(timestamp))
from messages
WHERE userid = "1234"
AND type = "Started/S"
Order by timestamp desc
limit 1))
我也得到一个错误,我无法理解
Error Code: 1093. You can't specify target table 'message' for update in FROM clause
答案 0 :(得分:0)
对于这样的查询,最好使用INSERT ... SELECT
语法。我认为类似这样的方法应该起作用:
insert into message (id, username, userid, message, type, timediff)
select id, username, userid, message, 'Ended/S', unix_timestamp() - unix_timestamp(tstamp)
from message m
where tstamp = (select MAX(tstamp)
from message m1
where m1.userid=1234 and m1.type='Started/S');
我在SQLFiddle上做了一个小演示,您可以在操作中看到它。