在MySQL中使用on查询更新多列

时间:2015-02-12 01:37:02

标签: mysql mysql-workbench

我需要在我的表'customers'中使用1 CASE命令设置city_populations列如下

Barrie      =   177,061
Toronto     =   2,480,000
Collingwood =   17,290
Thunder Bay =   108,359

我创建了2个查询,但它不会运行,任何建议。

UPDATE customers
SET city_population Barrie = 177061, 
                    Toronto = 2480000,
                    Collingwood = 17290, 
                    Thunder Bay =108359 
WHERE city = 'Barrie', 'Toronto', 'Collingwood', 'Thunder Bay';

 UPDATE customers
 SET city_population = 177061, 2480000, 17290, 108359 
 WHERE city = 'Barrie', 'Toronto', 'Collingwood', 'Thunder Bay';

2 个答案:

答案 0 :(得分:2)

尝试使用类似的东西

UPDATE `customers`
SET `city_population` = CASE `city`
WHEN 'Barrie' THEN 177061
WHEN 'Toronto' THEN 2480000
....
END,
WHERE `customers` IN ('Barrie', 'Toronto', ...);

答案 1 :(得分:0)

UPDATE `customers`
SET city_population = CASE city
WHEN 'Barrie' THEN 177061
WHEN 'Toronto' THEN 2480000
WHEN 'Collingwood' THEN 17290
WHEN 'Thunder Bay' THEN 108359  
END
WHERE `city` IN ('Barrie', 'Toronto', 'Collingwood', 'Thunder Bay');