如何更新MariaDB的不同表中的多个列

时间:2016-10-14 07:38:40

标签: mysql sql database mariadb

我正在使用MariaDB。我试图从SELECT不同的表更新两列。

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 )

但这不是......

我该怎么做? 请帮帮我......

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,使用合适的表达式/子查询/ etc)

您似乎正在更新User ??

中的所有行