I have a users table in my mysql database with columns like id, age and gender. I have inserted around 500 records into it.
Now i need to interchange the gender for the records, i.e., replace male with female and female with male.
I thought to do it this way:
update users set gender='female' where gender='male';
update users set gender='male' where gender='female';
But as you can see, as soon as i run the first query, all the records will be updated with the gender set to 'female'.
How can i modify the query or shall i go another way?
MySQL REPLACE() FunctionThe REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: This function performs a case-sensitive replacement.
Use the MySQL REPLACE() function to replace a substring (i.e. words, a character, etc.) with another substring and return the changed string. This function takes three arguments: The string to change.
update users set gender=case when gender='male' then 'female' else 'male' end where gender in ('male','female');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With