Click here to see the full article post
How about this one?
update salary as a
join salary as b on a.sex != b.sex
set a.sex = b.sex;
Also, we can do with the following query:
update salary set sex = IF(sex = 'm', 'f', 'm')
@Himanshu that's correct. 👍
@rfvbnju Looks very interesting!
Hah, I ended up using something over-complicated, with ELT and FIELD.
But before that I tried this, which is also a bit silly but which worked with the testcase and failed upon submission (4/8 test cases passed). I don't know why it failed, though, perhaps some sql_mode?
update salary sf, salary sm set sf.sex = 'm', sm.sex = 'f' where sf.sex = 'f' and sm.sex = 'm';
@rfvbnju I don't think this will work if all the rows have the same sex. i.e. the table contains only male or only female.
damn it! I was right at the very beginning! I submitted the update statement and got RUNTIME ERROR! Then I switched to select statement and I got another WRONG ANSWER!! DAMN IT!!
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.