Why is this mySQL UPDATE not making changes? - mysql

I know nothing about mySQL. A friend and I are struggling with this tiny bit of code.
Background:
I'm trying to solve an issue I have with character encoding on my WordPress database. My original WordPress database is wordpress, and the modified database is wordpress2 (which exists largely as a copy of the original). I want to select the post_content of a single entry in wordpress, convert it to utf8, and insert it into the corresponding row in wordpress2.
The Script:
update wordpress2.wp_posts wp2
set wp2.post_content = (select convert(wp.post_content using utf8)
from wordpress.wp_posts wp
where wp.ID = wp2.ID )
where wp2.ID = 789;
When run:
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
Why isn't this changing any rows? What do we need to do to make it do so?
Thank you.

The answer, as #zerkms first pointed out in the comments above, was that the values were already identical.

Related

MySQL update returns 0 rows affected while select returns results

select * from t_circle
where status = 0 and author_phone = 13511111111
and id in (
1,
2,
3
)
;
returns 3 rows with status of 0.
But the following update query with same conditions returns 0 rows affected:
udpate t_circle set status = 2
where status = 0 and author_phone = 13511111111
and id in (
1,
2,
3
);
Is there a reason for this? I have tried start transaction and commit, but still 0 rows affected. Other questions' answers suggest to run select first and make sure the rows are changed since if new value == old value, it is considered affected. I have excluded these 2 possibilities.
Note: this issue is found in our production server(Yeah I know I shouldn't but I have to) with InnoDB engine. I could modify the contents in GUI client like DBeaver and click save and the changes take effect, but not with sql statements. I wonder if it has anything to do with my account authorization?
Resolved! It is because I misspelled UPDATE. When I use mysql> in command line, update with 'udpate' just gives Query OK, 0 rows affected. My mysql's version is 5.7
I know that there is something wrong with the user permission of your client
By comparing GUI generated sql queries when I change something in GUI and my own queries. I found out the reason is I misspelled update to udpate. Now I begin to wonder how could mysql not complain about syntax error and just returns with 0 rows affected?
Well, at least now I know what to do when something odd happens.

Table named by a MySQL reserved word cannot be altered or dropped

Accidentally I named my table "RollUp" which is a reserved word in MySQL.
For some reason there was no error and the table had been successfully created.
Now I can SELECT, DESCRIBE etc. but I cannot ALTER, RENAME or DROP.
Backtick doesn't help.
How could I get rid of this table (either change its name or completely drop it)?
I cannot replicate this:
mysql> CREATE TABLE rollup (i INT NOT NULL);
Query OK, 0 rows affected (0.02 sec)
mysql> DROP TABLE rollup;
Query OK, 0 rows affected (0.01 sec)
So the problem was I did not close Python connection to the database.
Thank you and I am sorry for asking stupid question, it may be removed.

Does UPDATE overwrite values if they are identical?

I only want to update a vale in my database if it is different. Reading through the Oracle docs on UPDATE, it says...
...the UPDATE statement updates columns of existing rows in the named table with new values.
Since it doesn't say it won't overwrite identical values, should I take this statement literally? Does this mean MySQL does some sort of Boolean matching check for me?
No, MySQL won't overwrite identical values.
Lets say we insert some data:
insert into foo(id,val1,val2,val3) values (0,1,2,3);
Query OK, 1 row affected (0.00 sec)
If you update it with the same values:
update foo set id=0, val1=1, val2=2, val3=3 where id=0;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
Take a look on servers response 0 rows affected
an sql query would update even identical value by practically substituting them. Anyway, you can structure your sql to avoid it will substitute the identical value. (I think also that the latter way would be more time consuming then the normal procedure and maybe useless for the final result)

MYSQL UPDATE date field not working

Why won't this work:
USE presentations_db; UPDATE presentations_tbl SET `date` = '2012-12-13' WHERE `date` = '2013-12-12'
I have tried everywhere I could and can't find an answer.
date is the field name so used back ticks as required. date is of DATE data type.
I managed to get it to run through the commandline. I clicked on "command line client" and it asked for a password. I then ran the sql statement and got the following result:
mysql> UPDATE `presentations_db`.`presentations_tbl`SET date_ = '2012-12-13' WHERE date_ = '2013-12-12';
Query OK, 16 rows affected (0.06 sec) Rows matched: 16 Changed: 16 Warnings: 0
When I tried to run the same query by simply running mysql though a shell it came up with an error that the db can not be edited by localhost, which is more explanatory than 'Query interrupted'. This seems to be a gripe previously covered in http://bugs.mysql.com/bug.php?id=67766.
It would be nice if somone can tell me what I am doing wrong in workbench gui. I normally do the following when trying to run queries. I click on 'Edit Table Data' and the select the database and tables. It seems like I can view and run select queries but not update queries.

Why would rows match but not be changed with a mysql update statement?

I'm used to MSSQL, not Mysql, so sorry for this probably stupid question. I'm trying update my password for a backup database I'm getting setup by running the following query:
update users set password = md5('pass') where username = 'admin'
When I run this it says
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1 Changed: 0 Warnings: 0
Why is a row matching but not changing?
It means that the value was not changed. It was probably not changed because the column was already equal to the md5 hash of pass.