MySQL 更新返回失败
我正在运行 MySQL 更新查询,但它总是失败。我正在使用 WordPress,我还尝试从 phpMyAdmin 运行相同的查询,但也失败了。
这是我的表:
id | period | x | y |
---|---|---|---|
2021 年 9 月1 日 | - 2021 年 11 月 |
我尝试运行 get count:
SELECT COUNT(*) FROM `utility_period` WHERE 'period' = 'Sep 2021 - Nov 2021'
当记录存在于表中时,这会返回 0。
更新查询:
UPDATE `utility_period` SET total = '0’ WHERE `period` = ‘Sep 2021 – Nov 2021’
为什么会发生这种情况?
I am running a MySQL update query, but it always fails. I am using WordPress, I also tried to run the same query from phpMyAdmin which also fails.
This is my table:
id | period | x | y |
---|---|---|---|
1 | Sep 2021 - Nov 2021 |
I tried running get count:
SELECT COUNT(*) FROM `utility_period` WHERE 'period' = 'Sep 2021 - Nov 2021'
This returns 0 while the record exists in the table.
UPDATE Query:
UPDATE `utility_period` SET total = '0’ WHERE `period` = ‘Sep 2021 – Nov 2021’
Why is this happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tl;dr:
'
不是'
或'
。它们很难区分,但作为程序员你必须区分。在 MySQL 中,字符串文字用单引号
'
括起来。在此查询中,您被一个“有用”的文字处理程序欺骗了,该程序用
'
和'
替换了这些单引号。它们很难区分,但是'
和'
在 SQL(或大多数编程语言)中不能用作字符串封闭标记。并且,数据库、表和列的名称可以选择用反引号括起来,就像查询一样。仅当对象名称也是 SQL 中的关键字时才需要反引号。
所以
应该是这样
或这样
SQL 短语
WHERE 'period' = 'Sep 2021 - Nov 2021'
确定文本字符串常量'period'
是否等于常量“2021 年 9 月 - 2021 年 11 月”
。它不是。您在该列名称周围使用了'
而不是反引号。使用
$wpdb
意味着您将处理字符串文字的责任放在该模块上。它做得正确。tl;dr:
'
is not‘
or’
. They're hard to tell apart, but as a programmer you must.In MySQL, string literals are enclosed in single-quotes
'
.In this query you got skunked by a "helpful" word processing program that replaced those single quotes with
‘
and’
. They're very hard to tell apart, but‘
and’
are not useful as string-enclosing marks in SQL (or in most programming languages).And, the names of databases, tables, and columns are optionally enclosed in backticks, as your query does. The backticks are only required when an object name is also a keyword in SQL.
So this
should be this
or this
The SQL phrase
WHERE 'period' = 'Sep 2021 - Nov 2021'
determines whether the text string constant'period'
is equal to the constant'Sep 2021 - Nov 2021'
. It is not. You used'
around that column name rather than backticks.Your use of
$wpdb
means you put the responsibility for handling your string literals on that module. It does it correctly.谢谢大家。现在正在工作。
我无法识别问题,但现在再次使用 wpdb,问题得到了解决。
Thanks, everyone. It's working now.
I was not able to identify the issue, but now again used wpdb and the issue was resolved.