0

如何使用正确的以下查询的 SUM 更新表上total_talktime的行?使用分组?mobile_usersuser_id

这是选择查询:

SELECT SUM( TIME_TO_SEC( TIMEDIFF( `calls`.`finished_call` , `calls`.`start_call` ) ) ) AS `t` , `mobile_users`.`id`
FROM `mobile_users` 
LEFT JOIN `calls` ON `calls`.`user_id` = `mobile_users`.`id`
GROUP BY `mobile_users`.`id` 

我怎样才能使它与更新选择一起工作,包括分组?

4

3 回答 3

1

我假设您的查询正在运行试试这个

update table   
set total_talktime = (select t from  
(SELECT SUM (TIME_TO_SEC (TIMEDIFF ( `calls`.`finished_call` , `calls`.`start_call`  ) ) ) AS `t`, `mobile_users`.`id`
FROM `mobile_users` 
LEFT JOIN `calls` ON `calls`.`user_id` = `musers`.`id`
GROUP BY `mobile_users`.`id` ) as subquery)
于 2013-08-01T08:45:58.560 回答
0

我尚未测试查询,但希望这会有所帮助:

update table
set total_talktime  = derived.t
from table, 
(
SELECT SUM( TIME_TO_SEC( TIMEDIFF( `calls`.`finished_call` , `calls`.`start_call` ) ) ) AS `t` , `mobile_users`.`id`
FROM `mobile_users` 
LEFT JOIN `calls` ON `calls`.`user_id` = `mobile_users`.`id`
GROUP BY `mobile_users`.`id` 
) derived
where table.userId = derived.id
于 2013-08-01T08:52:49.547 回答
0

尝试:

UPDATE mobile_users SET total_talktime = (SELECT SUM( TIME_TO_SEC( TIMEDIFF( calls.finished_call , calls.start_call ) ) ) AS t , mobile_users.id FROM mobile_users LEFT JOIN calls ON calls.user_id = musers.id GROUP BY mobile_users.id )

于 2013-08-01T08:44:49.590 回答