我有以下语句,该语句需要再添加1个子查询以计算tweeps表中的关注者数量与ranking表中当前列的数量之间的差,并将该差插入名为latest的列中在ranking表中,确保PK是screenname,

像tweeps表中的跟随者coulm中的数字是10当前coulmn n排名表,相同的屏幕名称是5,该值将被添加到最新的+5

mysql_query ("
    INSERT  INTO ranking
    SELECT @rank := @rank + 1, tweeps.* FROM tweeps
    JOIN( SELECT @rank := 0 ) AS init
    ORDER BY followers DESC
    ON DUPLICATE KEY UPDATE
    ranking.ranking = @rank,
    ranking.name = tweeps.name,
    ranking.followers = tweeps.followers,
    ranking.tweets = tweeps.tweets,
    ranking.location = tweeps.location,
    ranking.`join date` = tweeps.join_date,
    ranking.avatar = tweeps.avatar;");
mysql_close($con);

最佳答案

尝试这个:

INSERT INTO ranking
SELECT @rank := @rank + 1, tweeps.* FROM tweeps
JOIN( SELECT @rank := 0 ) AS init
ORDER BY followers DESC
ON DUPLICATE KEY
UPDATE ranking set
ranking = @rank,
name = tweeps.name,
followers = tweeps.followers - followers,
tweets = tweeps.tweets,
location = tweeps.location,
`join date` = tweeps.join_date,
avatar = tweeps.avatar;


我将语法更改为ON DUPLICATE KEY UPDATE ranking set...

10-08 08:37