上面是我的表格设计,我需要从“单位”字段中减去值。我使用了这个查询
update table SET unit=unit-3 where product='Ghee' ORDER BY date DESC
上述查询使“unit”字段值位于 <minus>
中,如果酥油字段单位小于3..
这是场景
unit product date
1 ghee 2013-06-12
3 ghee 2013-06-14
我需要从产品 GHEE 中减去 3 个单位
如果我使用这个查询
update table SET unit=unit-3 where product='Ghee' ORDER BY date DESC
我需要按降序减去。
最佳答案
你能做这样的事情吗?
UPDATE table SET
unit=unit-(
SELECT TOP 1 unit
FROM table WHERE product = 'Ghee' ORDER BY UNIT DESC
)
where product='Ghee'
它将删除“3”或给定产品的最高单位值。
关于mysql查询以降序从表中减去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17130914/