mySQL UPDATE query returns “0 rows affected”

后端 未结 11 1092
感情败类
感情败类 2020-12-05 18:39

I have this query:

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

My table is phonecalls, I

相关标签:
11条回答
  • 2020-12-05 18:54

    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.

    0 讨论(0)
  • 2020-12-05 18:55

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

    0 讨论(0)
  • 2020-12-05 18:57

    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.
    0 讨论(0)
  • 2020-12-05 18:57

    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.

    0 讨论(0)
  • 2020-12-05 19:03

    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.

    0 讨论(0)
  • 2020-12-05 19:03

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

    0 讨论(0)
提交回复
热议问题