MySQL Insert query doesn#39;t work with WHERE clause(MySQL 插入查询不适用于 WHERE 子句)
问题描述
这个查询有什么问题:
INSERT INTO Users( weight, desiredWeight ) VALUES ( 160, 145 ) WHERE id = 1;
它可以在没有 WHERE 子句的情况下工作.我似乎忘记了我的 SQL.
It works without the WHERE clause. I've seemed to have forgot my SQL.
推荐答案
MySQL INSERT 语法 不支持 WHERE 子句,因此您的查询将失败.假设您的 id 列是唯一的或主键:
MySQL INSERT Syntax does not support the WHERE clause so your query as it stands will fail. Assuming your id column is unique or primary key:
如果您尝试插入 ID 为 1 的新行,您应该使用:
If you're trying to insert a new row with ID 1 you should be using:
INSERT INTO Users(id, weight, desiredWeight) VALUES(1, 160, 145);
如果您尝试更改 ID 为 1 的现有行的 weight/desiredWeight 值,您应该使用:
If you're trying to change the weight/desiredWeight values for an existing row with ID 1 you should be using:
UPDATE Users SET weight = 160, desiredWeight = 145 WHERE id = 1;
如果你愿意,你也可以像这样使用 INSERT .. ON DUPLICATE KEY 语法:
If you want you can also use INSERT .. ON DUPLICATE KEY syntax like so:
INSERT INTO Users (id, weight, desiredWeight) VALUES(1, 160, 145) ON DUPLICATE KEY UPDATE weight=160, desiredWeight=145
或者甚至像这样:
INSERT INTO Users SET id=1, weight=160, desiredWeight=145 ON DUPLICATE KEY UPDATE weight=160, desiredWeight=145
同样重要的是要注意,如果你的 id 列是一个自动增量列,那么你最好从你的 INSERT 中省略它,让 mysql 像往常一样增加它.
It's also important to note that if your id column is an autoincrement column then you might as well omit it from your INSERT all together and let mysql increment it as normal.
这篇关于MySQL 插入查询不适用于 WHERE 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 插入查询不适用于 WHERE 子句
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
