开发者

How to update a column based on comparison result on another column

I can easily locate the rows I want with these sql statements:

SELECT COUNT(*) FROM `individuals` WHERE `company_zip` != '';
SELECT COUNT( * ) FROM `individuals` WHERE length( `company_zip` ) > 0;
SELECT COUNT( * ) FROM `individuals` WHERE strcmp( `company_zip`, '' ) != 0;

And there are probably 100 other ways to do this.

However...

Try using these in an UPDATE statement

UPDATE `individuals` SET `company_country` = 1 WHERE `company_zip` != '';
UPDATE `individuals` SET `company_country` = 1 WHERE length( `company_zip` ) > 0;
UPDATE `individuals` SET `company_country` = 1 WHERE strcmp( `company_zip`, '' ) != 0;

And I get responses like this:

0 row(s) affected. ( Query took 0.5920 sec )
开发者_StackOverflow

I admit I am tired from looking at manual pages and google searches to figure this out. Which mysql principle am I missing here? It is easy to count the lines in SELECT statement but same criteria does not work for UPDATE statement. Is this a bug?

Darn! I figured it out just before posting. So I will give anyone wondering the answer. I accidentally added field company_country with default value of 1 and so all records got default value of '1'. So mysql was trying to update fields, but found that they already were set to 1. Blanking them out allowed UPDATE queries to work. Nevermind. Posting for anyone who makes similar error.

Bradley


OK. To be specific, because all records already had field 'company_country' set to 1, mysql was telling me in a terse way, that it had not UPDATEd or changed any of the records. They started out with that field set to 1, and ended up with that field set to 1. Check your defaults people, especially is someone else set up the table. I could have typed

UPDATE `individuals` SET `company_country` = 1;

and the answer would still have been the same. 0 row(s) affected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜