Reputation: 63
I am trying to update values in a MySQL table and really getting stuck.
Basically I want to update a column's value to 1 where another column (in the same row) = "N". It should be quite simple but I can't fathom it.
UPDATE household SET allowsDogs=1 WHERE allowsCats="N"
In my mind the above query should, for each household if allowsCats="N" then set allowsDogs to 1. But instead I get an empty result set.
I have also tried variations:
Update household set allowsDogs=1 where householdID in (select householdID from household where allowsCats="N")
Update household set allowsDogs=1 where householdID in (select householdID from copy_of_household where copy_of_household.allowsCats="N")
I'm just about to write a php script to read in each row and update one at a time....But there must be an easier way...
Upvotes: 1
Views: 720
Reputation: 1269543
Presumably, you mean one of the following:
UPDATE household
SET allowsDogs = 1
WHERE allowsCats = 0;
or
UPDATE household
SET allowsDogs = 'Y'
WHERE allowsCats = 'N';
Mixing numbers and characters for flags is like, well, mixing cats and dogs.
Upvotes: 2
Reputation: 168988
The syntax you have specified is correct, and should work. I believe this is where you are getting tripped up:
But instead I get an empty result set.
UPDATE
queries do not return any result set. They do their work and then return an empty result set. However, your client library or application should provide a way for you to see how many records were altered (but not which specific ones).
Further, note that the database server may skip updating a record if all of the fields you are updating already have the new value you are assigning. For example, if all of the records in the table with the field allowCats
equal to "N"
also have their allowDogs
field equal to 1
then the database server may not include those rows in the total number of rows updated, since they were not actually changed.
Upvotes: 0
Reputation: 11061
Your first query is correct, but it should not return result set.
To see the result use separate SELECT * FROM household
statement.
Upvotes: 0