UPDATE customer SET OrderNo = ( SELECT afterOrder.rowNum FROM ( SELECT @row_number := CASE WHEN @customer_no = s.DepartmentId THEN @row_number + 1 ELSE 1 END AS rowNum, @customer_no := s.DepartmentId AS stu_DepartmentId, s.id FROM customer s, ( SELECT @row_number := 0, @customer_no := '039c235a-2039-44b0-84b6-bb9586092b78' ) t ORDER BY s.DepartmentId, s.CreatedTime ) afterOrder WHERE afterOrder.id = customer.Id );
mysql 用select后的值 更新表时千万别用这种坑人的写法
UPDATE customer INNER JOIN ( SELECT @row_number := CASE WHEN @customer_no = s.DepartmentId THEN @row_number + 1 ELSE 1 END AS rowNum, @customer_no := s.DepartmentId AS stu_DepartmentId, s.id FROM customer s, ( SELECT @row_number := 0, @customer_no := '039c235a-2039-44b0-84b6-bb9586092b78' ) t ORDER BY s.DepartmentId, s.CreatedTime ) afterOrder ON customer.id = afterOrder.id SET customer.OrderNo = afterOrder.rowNum