MYSQL 使用变量更新多列

发布于 2024-11-25 07:21:33 字数 469 浏览 2 评论 0原文

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

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

但是,我尝试使用相同的格式进行更新:

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

...并且抛出语法错误:

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 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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

榆西 2024-12-02 07:21:33

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

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

尽管这可能不安全。您应该查看参数化查询

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

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

Though this may be insecure. You should look into parameterized queries.

话少心凉 2024-12-02 07:21:33
INSERT INTO products ($fields) VALUES ($values) ON DUPLICATE KEY UPDATE field = VALUES(field), ...

不要忘记唯一键或主键

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

Don't forgot about unique or primary key

凉栀 2024-12-02 07:21:33

你需要一个=

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

you need an =

UPDATE products SET ($fields) = $values WHERE sku = '$checksku'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文