Mysql 执行此选择和更新循环的更有效方法

Mysql 执行此选择和更新循环的更有效方法,mysql,sql,Mysql,Sql,这是我的投票表 "id" "votedElm" "voteType" "voteProcessed" "country" "3" "6" "1" "0" "US"//1-1=0 "4" "8" "0" "0" "US"//2+0-1=1 "9" "8" "1" "0" "US" "5"

这是我的投票表

"id"    "votedElm"  "voteType"  "voteProcessed" "country"
"3"     "6"         "1"         "0"             "US"//1-1=0

"4"     "8"         "0"         "0"             "US"//2+0-1=1
"9"     "8"         "1"         "0"             "US"

"5"     "9"         "0"         "0"             "US"//2+0-1=1
"10"    "9"         "1"         "0"             "US"
这是我的桌子
喜欢的

  "id"      "type"  "parent"    "country"   "votes"
    6       10      3           US          1
    8       10      7           US          2
    9       10      7           US          2
我在以下事件中更新表
likes

//Pseudocode - This actually is inside an mysql scheduled event
//Select all the votes
select id, votedElm, voteType, country from votes
if voteType = 0 then

update likes set votes=votes+1 where id=votedElm and country=country
update votes set voteProcessed = 1 where id = id

elseif voteType = 1 then

update likes set votes=votes-1 where id=votedElm and country=country
update votes set voteProcessed = 1 where id = id

End If
这一切一次只发生一排。您认为有更好、更有效的方法来执行sql吗

活动内容如下:

BEGIN
     DECLARE vId INT(10) DEFAULT '0';
     DECLARE vElm INT(10) DEFAULT '0';
    DECLARE vType TINYINT(1) DEFAULT '0';
    DECLARE vProcessed TINYINT(1) DEFAULT '0';
    DECLARE vCountry VARCHAR(2) DEFAULT "";
    DECLARE updateDone INT DEFAULT FALSE;

    -- declare cursor for employee email
    DEClARE updater CURSOR FOR
        SELECT id, votedElm, voteType, voteProcessed, country FROM votes;

    -- declare NOT FOUND handler
    DECLARE CONTINUE HANDLER 
        FOR NOT FOUND SET updateDone = TRUE;

    OPEN updater;

    doUpdate: LOOP

        FETCH updater INTO vId, vElm, vType, vProcessed, vCountry;

        IF updateDone THEN
            LEAVE doUpdate;
        END IF;

        -- update likes
        UPDATE likes
                INNER JOIN votes
                    ON votes.vElm = likes.id AND votes.vCountry = likes.country
                    SET
                    likes.votes = IF(votes.vType = 0,likes.votes+1,likes.votes-1),
                    votes.vId = 1;

    END LOOP doUpdate;

    CLOSE updater;

END

您只需执行3个更新:

update likes set votes=votes+1 where voteType = 0
update likes set votes=votes-1 where voteType = 1
update votes set voteProcessed = 1
这里假设您只有voteType=0,1,所以您可以处理所有的内容。

您可以尝试以下方法:

UPDATE
    likes
INNER JOIN votes
    ON votes.votedElm = likes.id
    AND votes.country = likes.country
SET
    likes.votes = IF(votes.vote_type = 0,likes.votes+1,likes.votes-1),
    votes.voteProcessed = 1
WHERE
    votes.voteProcessed = 0

您可以阅读更多关于多个更新的信息

@codedad假设在likes表中已经有关于投票中所有可能性的条目。votedElm和Voces.country?@Stephan:thx指向该事实;)在这种情况下,我支持你的解决方案,因为它是正确的@codedad thx:)要使查询正常工作,您只需加入
投票
表格是的,稍后a会意识到这一点,但如果我进行修改,更新看起来会比您的解决方案更复杂;)但是,为了不误导读者,我将改变我的想法,我正在经历一段艰难的时间,让它在我的活动中发挥作用。我对列名和变量名都感到困惑。你能看看我的活动吗?我在问题中发布了它。您正在尝试对
投票
表中的每一行执行此查询。。。但是这个查询会更新所有需要的行,请尝试单独执行它,或者删除循环,因为您不需要itThanks。现在可以了。是否可以将voteProcessed=0时的
添加到sql中,以便它仅在voteProcessed=0时拾取行?如果其为1,则其已被处理。不需要处理这些。