任何方式将值从一个单元格传输到另一个? [英] Any way to transfer value from one cell to another?
问题描述
在SQL语言或MySQL(或其他DBMA)中有什么方法可以将值从一个单元传输到另一个单元?例如,假设有一个名为user_cars的表具有以下结构:
Is there any way in the SQL language or in MySQL (or other DBMA) to transfer a value from one cell to another? For example, say there is a table called user_cars with the following structure:
|id| |user_name| |num_cars|
Bob有5辆车,John有3辆车。有没有办法在一个查询减去2车从鲍勃和添加2到约翰?我知道这可以用两个更新查询,但我只想知道是否有更有效的方式。
Bob has 5 cars, and John has 3 cars. Is there any way to in one query subtract 2 cars from Bob and add 2 to John? I know this can be done with two update queries, but I'd just like to know if there was a more efficient way.
推荐答案
对于Oracle,你可以这样做。不知道在mysql中是否有等价的。显然,这个特定的语句非常特定于你所说的例子。
For Oracle you could do this. Don't know if there is an equivalent in mysql. Obviously this particular statement is very specific to the example you stated.
UPDATE user_cars
SET num_cars = num_cars +
CASE WHEN user_name='Bob' THEN -2
WHEN user_name='John' THEN +2
END
WHERE user_name IN ( 'Bob', 'John' )
这篇关于任何方式将值从一个单元格传输到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!