我有一个5列表(列是id,answer,weight,quiz_id和question_id) 我想用question_id对它进行排序,然后按weihgt排序,然后我想根据这个双重排序逐步重新编号id列。 是不是可以在mysql(我使用phpmyadmin)?
感谢
类似的东西:
SELECT
@curRow := @curRow + 1 AS id,
T.Answer, T.Weight, T.Quiz_Id, T.Question_Id
FROM wp_wpss_Answers T
JOIN (SELECT @curRow := 0) r
ORDER BY QuestioN_Id, Weight
LIMIT 0 , 1000
答案 0 :(得分:0)
假设您只想在查询中显示行号,请使用以下内容:
SELECT
@curRow := @curRow + 1 AS RowNumber,
T.Answer, T.Weight, T.Quiz_Id, T.Question_Id, T.Id
FROM YourTable T
JOIN (SELECT @curRow := 0) r
ORDER BY QuestioN_Id, Weight
如果您想更新自己的ID,可以使用上述内容,但我会问你为什么要这样做?我看不出有任何理由重新编号您的ID列 - 将其设置为Identity并让MySQL处理它。
但是,要更新您的Id列,请尝试以下操作:
UPDATE YourTable t
JOIN (SELECT
@curRow := @curRow + 1 AS RowNumber,
T.Answer, T.Weight, T.Quiz_Id, T.Question_Id, T.Id
FROM YourTable T
JOIN (SELECT @curRow := 0) r
ORDER BY QuestioN_Id, Weight) T2 ON T.Id = T2.Id
SET t.Id = T2.RowNumber
这是Fiddle。
但你真的应该重新考虑这种方法。
祝你好运。