我有一个表,其中存储多个联赛的排名信息,可以将其视为一个幻想网站。按列的结构如下。
league_id | user_id | total_points | prediction_difference | current_position | last_position
为了计算当前排名,我发出以下查询:
SELECT
*
FROM f_u_standings
WHERE league_id = 1
ORDER BY total_points DESC,
prediction_difference DESC
我的问题是,现在我有了这个结果集,如何根据更新 current_position 列的 SELECT
查询执行 UPDATE
?我在这个项目中选择的编程语言是 PHP。
最佳答案
您可以使用选择进行更新..这假设您每行都有一个 ID
UPDATE TABLE f_u_standings fs,
(
SELECT
*
----- do what you want to change current_position -----
FROM f_u_standings
WHERE league_id = 1
ORDER BY total_points DESC,
prediction_difference DESC
) temp
SET fs.current_position = temp.current_position WHERE fs.id = temp.id
关于php - 根据同一个表的查询更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24762291/