使用 MySQL 中的查询更新多列
UPDATE multiple columns using on query in MySQL
我需要在 table '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';
尝试使用类似这样的东西
UPDATE `customers`
SET `city_population` = CASE `city`
WHEN 'Barrie' THEN 177061
WHEN 'Toronto' THEN 2480000
....
END,
WHERE `customers` IN ('Barrie', 'Toronto', ...);
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');
我需要在 table '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';
尝试使用类似这样的东西
UPDATE `customers`
SET `city_population` = CASE `city`
WHEN 'Barrie' THEN 177061
WHEN 'Toronto' THEN 2480000
....
END,
WHERE `customers` IN ('Barrie', 'Toronto', ...);
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');