此查询选择所需的结果:它为每个id添加行号,按id分组,从每个id再次从1开始:
SELECT `id_product`,
`lowest_price`,
IF(@CG=`id_product`, @I:=@I+1, @I:=1) AS rownr,
@CG:=`id_product` AS run_order
FROM `table`
INNER JOIN (SELECT @I:=0, @CG:=-1) x
ORDER BY id_product ASC, lowest_price ASC
我无法更新表格:
更新table
SET row_number = .....
用临时表解决了这个问题,但还有更整洁的解决方案吗?