我有一张桌子,可以存储多个联赛中的积分信息,将其视为幻想网站。根据列,结构如下。
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列的UPDATE
查询执行SELECT
?我在这个项目中选择的编程语言是PHP。
答案 0 :(得分:1)
您可以使用选择更新..这假设您有每行的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
答案 1 :(得分:1)
这可能更接近您的需求:
UPDATE f_u_standings fs,
(SELECT @rownum:=@rownum+1 rownum, id
FROM f_u_standings, (SELECT @rownum := 0) init
WHERE league_id = 1
ORDER BY total_points DESC,
prediction_difference DESC) temp
SET fs.current_position = temp.rownum
WHERE fs.id = temp.id