MYSQL 使用变量更新多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6765331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:34:02  来源:igfitidea点击:

MYSQL Updating multiple columns using variables

mysqlvariablesvariable-assignmentmultiple-columns

提问by Brett

I used this query to insert all my values into this database:

我使用这个查询将我所有的值插入到这个数据库中:

INSERT INTO products ($fields) VALUES ($values)

However, I try to use the same format for UPDATE:

但是,我尝试对 UPDATE 使用相同的格式:

UPDATE products SET ($fields) VALUES ($values) WHERE sku = '$checksku'

...and am getting thrown a syntax error:

...并且我被抛出一个语法错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('product,make,model,' at line 1

I can't figure it out. Would appreciate any help. Thanks.

我想不通。将不胜感激任何帮助。谢谢。

回答by Nick Rolando

UPDATE syntax is different than INSERT syntax. An example of UPDATE would be:

UPDATE 语法与 INSERT 语法不同。UPDATE 的一个例子是:

"UPDATE products SET field1 = 'value1', field2 = '$val2', field3 = 5 WHERE sku = '$checksku'"

回答by Павел Зорин

INSERT INTO products ($fields) VALUES ($values) ON DUPLICATE KEY UPDATE field = VALUES(field), ...

Don't forgot about unique or primary key

不要忘记唯一键或主键

回答by Tom Squires

you need an =

你需要一个 =

UPDATE products SET ($fields) = $values WHERE sku = '$checksku'