2017-06-12 112 views
1

後後,我選擇了從其他4個表值如何更新一個表:更新選擇MySQL的

SELECT 
(SELECT COUNT(*) FROM racunala) as racunala, 
(SELECT COUNT(*) FROM objekti) as objekti, 
(SELECT COUNT(*) FROM programi) as programi, 
(SELECT COUNT(*) FROM users) as korisnici, 
(SELECT COUNT(*) FROM tvrtke) as tvrtke 

我呼籲另一個表「累積」,我想更新基於上面的查詢該表。

UPDATE `info`.`cumulative` SET `korisnici`=korisnici, `tvrtke`=tvrtke, `objekti`=objekti, `programi`=programi, `racunala`=racunala WHERE `idcumulative`='1'; 

我想製作一個存儲過程,它將更新「累積」表中的所有數據。

回答

2

你需要類似的東西

UPDATE 
    `info`.`cumulative` 
SET 
    `korisnici`= (SELECT COUNT(*) FROM users), 
    `tvrtke`= (SELECT COUNT(*) FROM tvrtke), 
    `objekti`= (SELECT COUNT(*) FROM objekti), 
    `programi`= (SELECT COUNT(*) FROM programi), 
    `racunala`= (SELECT COUNT(*) FROM racunala) 
WHERE 
    `idcumulative`='1'; 

你可以用WHERE條件

UPDATE 
    `info`.`cumulative` 
SET 
    `korisnici`= (SELECT COUNT(*) FROM users WHERE users.X = info.X), 
    `tvrtke`= (SELECT COUNT(*) FROM tvrtke WHERE tvrtke.X = info.X), 
    `objekti`= (SELECT COUNT(*) FROM objekti WHERE objekti.X = info.X), 
    `programi`= (SELECT COUNT(*) FROM programi WHERE programi.X = info.X), 
    `racunala`= (SELECT COUNT(*) FROM racunala WHERE racunala.X = info.X) 
WHERE 
    `idcumulative`='1'; 
+0

謝謝主席先生結合起來。這正是我需要的 – RaceLt