2013-08-01 33 views
0

如何更新mobile_users表中的total_talktime行,並使用以下查詢的SUM與正確的user_id?使用組?如何使用group更新選擇查詢?

這是Select查詢:

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` 

我怎麼能使其與更新選擇,由含組工作?

回答

0

嘗試:

UPDATE mobile_users SET total_talktime = (SELECT SUM(TIME_TO_SEC(TIMEDIFF(callsfinished_callcallsstart_call)))AS tmobile_usersid FROM mobile_users LEFT JOIN calls ON callsuser_id = musersid GROUP BY mobile_usersid

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) 
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