mySQL UPDATE 查询返回“0 rows受影响”

发布于 2024-08-20 04:49:43 字数 266 浏览 6 评论 0原文

我有这样的查询:

UPDATE phonecalls 
   SET Called = "Yes" 
 WHERE PhoneNumber = "999 29-4655"

我的表是 phonecalls,我有一个名为 PhoneNumber 的列。我想要将名为 Called 的列更新为“yes”。

知道我做错了什么吗?当我返回查询时,它说 0 行受影响。

I have this query:

UPDATE phonecalls 
   SET Called = "Yes" 
 WHERE PhoneNumber = "999 29-4655"

My table is phonecalls, I have a column named PhoneNumber. All I want to update is a column named Called to "yes".

Any idea what I am doing wrong? when I return my query it says 0 rows affected.

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

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

发布评论

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

评论(11

不奢求什么 2024-08-27 04:49:43

如果该值已经存在,mysql将不会更改它,因此将返回“0 rows受影响”。因此,请务必检查 called 的当前值

If the such value already exists, mysql won't change it and will therefore return "0 rows affected". So be sure to also check the current value of called

安稳善良 2024-08-27 04:49:43

我观察到 0 受影响行的另一个原因是:数据类型错误。如果您要更新的列是整数或布尔值,并且您将其设置为字符串,则它将不会更新 - 但您也不会收到错误。

总结本文中的其他策略/想法:

  1. 使用 SELECT 语句检查您的 WHERE 是否有效并返回结果。
  2. 检查您的列是否已经具有您要设置的值。
  3. 检查您所需的值是否适合列的数据类型。

Another reason for 0 affected rows that I have observed: wrong data type. If the column you want to update is an integer or boolean, and you set it to a string, it won't be updated - but you will also get no error.

To sum up the other strategies/ideas from this post:

  1. Check with a SELECT statement, whether your WHERE works and returns results.
  2. Check whether your columns do already have the value you want to set.
  3. Check if your desired value suits the data type of the column.
も让我眼熟你 2024-08-27 04:49:43

如果值相同,MySQL 将不会更新该行(不会触发任何警告或错误),因此受影响的行计数将为 0。

If the values are the same, MySQL will not update the row (without triggering any warning or error), so the affected row count will be 0.

記柔刀 2024-08-27 04:49:43

问题可能是没有 PhoneNumber == "999 29-4655" 的记录。

尝试这个查询:

SELECT * FROM phonecalls where PhoneNumber = '999 29-4655'

如果它没有返回任何内容,则没有匹配的行。

The problem might be that there are no records with PhoneNumber == "999 29-4655".

Try this query:

SELECT * FROM phonecalls where PhoneNumber = '999 29-4655'

If it doesn't return anything, then there are no rows that match.

情绪操控生活 2024-08-27 04:49:43

为了 Google 的任何人的利益,这个问题是由我引起的,因为我试图使用 CONCAT() 附加到一个空字段。

UPDATE example SET data=CONCAT(data, 'more');

如果 dataNULL,则 CONCAT() 返回 NULL(忽略第二个参数),因此该值不不更改(将 NULL 值更新为 NULL 值),因此更新了 0 行。

在这种情况下,更改为 CONCAT_WS() 函数反而解决了问题。

For the benefit of anyone here from Google, this problem was caused by me because I was trying to append to an empty field using CONCAT().

UPDATE example SET data=CONCAT(data, 'more');

If data is NULL, then CONCAT() returns NULL (ignoring the second parameter), so the value does not change (updating a NULL value to be a NULL value), hence the 0 rows updated.

In this case changing to the CONCAT_WS() function instead fixed the problem.

美人迟暮 2024-08-27 04:49:43

尝试 select count(*) from phonecalls where PhoneNumber = "999 29-4655"; 这将为您提供匹配行的数量。如果结果为 0,则数据库中没有匹配的行。-

Try select count(*) from phonecalls where PhoneNumber = "999 29-4655"; That will give you the number of matching rows. If the result is 0, then there isn't a row in the database that matches.-

流云如水 2024-08-27 04:49:43

检查以确保返回一些结果。

SELECT * FROM phonecalls WHERE PhoneNumber = '999 29-4655'

如果它没有返回任何结果,则说明过滤器 WHERE PhoneNumber = '999 29-4655' 不正确。

Check to make sure this returns some result.

SELECT * FROM phonecalls WHERE PhoneNumber = '999 29-4655'

If it doesn't return any result than the filter WHERE PhoneNumber = '999 29-4655' is not correct.

浪漫人生路 2024-08-27 04:49:43
  1. 它是否说匹配行:1 更改:0 警告:0?那么也许它已经设置为该值。
  2. 您尝试过单引号与双引号吗?
  3. “999 29-4655”是空格还是制表符,它在您的查询和数据库中是否一致?
  1. Does it say Rows matched: 1 Changed: 0 Warnings: 0? Then maybe it's already set to that value.
  2. Did you try single quotes vs. double quotes?
  3. "999 29-4655" is the space a space or a tab and is it consistent in your query and the database?
无名指的心愿 2024-08-27 04:49:43

这是我的建议:

UPDATE `phonecalls` SET `Called` = 'yeah!' WHERE `PhoneNumber` = '999 29-4655' AND `Called` != 'yeah!'

并确保表和字段的名称区分大小写。

That's my sugestion:

UPDATE `phonecalls` SET `Called` = 'yeah!' WHERE `PhoneNumber` = '999 29-4655' AND `Called` != 'yeah!'

And make sure with the case-sensitive name of table and field`s.

幻想少年梦 2024-08-27 04:49:43

刚刚遇到了一个不起眼的案例。我们的代码从数据库中读取记录列表,更改一列,然后将它们一一写回。 UPDATEWHERE 子句仅包含两个条件:WHERE key=?并且last_update_dt=?。 (时间戳检查用于乐观锁定:如果在我们写入记录之前记录被另一个进程更改,则更新 0 行并引发错误。)

但是对于某一特定行,UPDATE 失败 -影响零行。

经过一番绞尽脑汁后,我注意到该行的时间戳是2019-03-10 02:59。在美国大部分地区,该时间戳不存在 - 夏令时导致时间直接从 2:00 跳到 3:00。所以我猜测,在从 MySQL 到 Java 再到 MySQL 的往返过程中,代码的某些部分对时间戳的解释与其他部分不同,导致 WHERE 子句中的时间戳不匹配。

将行的时间戳更改一小时可以避免该问题。

(当然,正确的解决方法是废除夏令时。我创建了 Jira,但美国政府尚未对此做出回应。)

Just ran into an obscure case of this. Our code reads a list of records from the database, changes a column, and writes them back one by one. The UPDATE's WHERE clause contains only two conditions: WHERE key=? AND last_update_dt=?. (The timestamp check is for optimistic locking: if the record is changed by another process before we write ours, 0 rows are updated and we throw an error.)

But for one particular row the UPDATE was failing- zero rows effected.

After much hair-pulling I noticed that the timestamp for the row was 2019-03-10 02:59. In much of the U.S. that timestamp wouldn't exist- Daylight Savings Time causes the time to skip directly from 2:00 to 3:00. So I guessed that during the round trip from MySQL to Java back to MySQL, some part of the code was interpreting that timestamp differently from the rest, making the timestamps in the WHERE clause not match.

Changing the row's timestamp by one hour avoided the problem.

(Of course, the correct fix is to abolish Daylight Savings Time. I created a Jira but the U.S. Government has not responded to it yet.)

深居我梦 2024-08-27 04:49:43

就我而言,我试图更新一列文本以纠正截断问题。尝试更新为正确的文本会产生 0 rows Updated,因为该行中的文本没有更改。

一旦我扩展了表结构中的列以容纳正确的字符数,我就能够看到所需的结果。

In my case, I was trying to update a column of text to correct a truncation problem with it. Trying to update to the correct text was yielding 0 rows updated because the text in the row wasn't changing.

Once I extended the column in the table structure to accommodate for the correct number of characters, I was able to see the desired results.

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