如果我有一组数字存储在表列中,例如 1,60,71
如何让我的 sql 使用 while 或存储过程中的其他方法循环遍历这组数字?
我喜欢做
Loop throught 1,60 and 71
Update row where id = 1,60 and 71 with my data.
我希望你能明白。
编辑: 更新前的表格
"id" "total" "vals"
"1" "0" ""
"2" "0" ""
"3" "0" ""
"4" "0" "1,2,3"
更新后
"id" "total" "vals"
"1" "1" ""
"2" "1" ""
"3" "2" ""
"4" "0" "1,2,3"
自 vals
的id 4
是1,2,3
- ID 为 1,2,3 的行已由 +1
更新
唯一的问题是遍历该 vals 字符串中的每个值,并将相应的行更新 +1
最佳答案
使用 JOIN,而不是循环:
UPDATE Table t1
JOIN Table t2
ON FIND_IN_SET(t1.id, t2.vals)
SET t1.total = t1.total + 1
WHERE t2.id = 4
关于MySql 在存储过程中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19074453/