Several times recently I have asked the students to develop on nails, such as the following figure:
The problem is summed up: in which MySQL update a record syntax is correct, but the record has not been updated ...
Just encountered this problem, I got this statement directly carried out in a test library which found that there is a problem, but still developed and described differently , and here I use the test data to simulate follows:
Problematic SQL statement:
update apps set owner_code='43212' and owner_name='李四' where owner_code='13245' and owner_name='张三';
The previous record of execution is as follows:
After recording the execution is as follows:
Can be seen, not as the result of the development of the students said, "it does not seem to effect" actually have the effect of:
owner_name的值没有变,但owner_code变成了0!
why?
It seems that grammar is no problem, update MySQL syntax rolled official document:
See assignment_list format is a comma-separated list of col_name = value, suddenly all of a sudden, the students want to develop multi-field update statement should read:
update apps set owner_code='43212' , owner_name='李四' where owner_code='13245' and owner_name='张三';
And then go back a re-test:
Sure enough, this case has been the desired result!
Summary : In an UPDATE statement, if you want to update multiple fields, between fields can not use the "AND", but should be separated by commas.
Postscript : empty behind and so, when they look back for a moment, why use "AND" separated by time, owner_code = 0 will appear strange results? After repeatedly trying to find:
update apps set owner_code='43212' and owner_name='李四' where owner_code='13245' and owner_name='张三';
Equivalent to:
update apps set owner_code=('43212' and owner_name='李四') where owner_code='13245' and owner_name='张三';
And ( '43212' and owner_name = 'John Doe') is a logical expression, and here is not difficult to know owner_name 'John Doe'. Thus, the result is a logical expression to false , to false in MySQL equivalent to 0!