假设我有这些专栏
uniqueID|Money|Quantity|MoneyOrder|QuantityOrder
1|23|12||
2|11|9||
3|99|100||
我想要做的是根据MoneyOrder
的值更新QuantityOrder
和ORDER BY
。
结果将是:
uniqueID|Money|Quantity|MoneyOrder|QuantityOrder
1|23|12|2|1
2|11|90|1|2
3|99|100|3|3
我希望更新像标识列一样运行,而不是实际使其成为标识列。我知道我可以按'x'排序,订单就是结果,但我想生成一个报告,你可以逐行看到该项目。
这样的事情可能update mytable set Moneyorder = 'imnotsure' order by MoneyOrder asc
吗?
答案 0 :(得分:35)
SET @rownumber = 0;
update mytable set Moneyorder = (@rownumber:=@rownumber+1)
order by MoneyOrder asc
或者在单个查询中执行此操作
update mytable target
join
(
select id, (@rownumber := @rownumber + 1) as rownum
from mytable
cross join (select @rownumber := 0) r
order by MoneyOrder asc
) source on target.id = source.id
set Moneyorder = rownum
答案 1 :(得分:7)
查看这个问题的答案:
Updating column so that it contains the row position
SET @counter = 0;
UPDATE
my_table
SET MoneyOrder = @counter := @counter + 1
ORDER BY Money;
SET @counter = 0;
UPDATE
my_table
SET QuantityOrder = @counter := @counter + 1
ORDER BY Quantity;