2016-10-14 52 views
0

我正在使用MariaDB。我試圖從SELECT不同的表中更新兩列。如何從MariaDB的不同表中更新多列

UPDATE User U 
SET 
    U.UserPoint = (
     SELECT ((SELECT COUNT(*) 
       FROM CARD_COMM R 
       WHERE R.Card_ID = C.Card_ID) * 3 
       + 
       (SELECT COUNT(*) 
       FROM SECTION_CARD_LIKE L 
       WHERE L.Card_ID = C.Card_ID) * 1) as userPoint 
       FROM CARD C WHERE C.userid = U.userid ORDER BY userPoint DESC limit 1) 

這個作品

UPDATE User U 
SET 
    (U.UserPoint, U.Card) = (
     SELECT ((SELECT COUNT(*) 
       FROM CARD_COMM R 
       WHERE R.Card_ID = C.Card_ID) * 3 
       + 
       (SELECT COUNT(*) 
       FROM SECTION_CARD_LIKE L 
       WHERE L.Card_ID = C.Card_ID) * 1) as userPoint, 
       C.Card_ID as card 
       FROM CARD C WHERE C.userid = U.userid ORDER BY userPoint DESC limit 1) 

但這個劑量不....

如何做到這一點? 請幫我...

+0

不確定你想要做什麼?在第一個示例中,您在一列中插入單個值。在第二個示例中,您嘗試相同,但指定兩列但只有一個值? –

回答

0

使用多表更新,像

UPDATE User 
    JOIN (SELECT userid, up_value, card_value ...) AS x 
     ON x.userid = User.userid 
    SET User.UserPoint = x.up_value, 
     User.Card = x.card_value; 

(用合適的表達式/子查詢/等,爲up_value & card_value)

你似乎更新所有用戶行

相關問題