简体   繁体   English

在MySQL表中用'b'更新'a'记录,用'a'更新'b'

[英]In MySQL table update 'a' record with 'b' and 'b' with 'a'

In my table there are two field, one is name and other is gender. 在我的表中有两个字段,一个是名字,另一个是性别。 I want to fire query so that every male is update with female and viceversa. 我想解雇查询,以便每个男性都与女性一起更新,反之亦然。

I don't want to use procedure, trigger or function. 我不想使用程序,触发器或功能。 I have to do this only with simple query. 我只能通过简单的查询来做到这一点。

Make it a three step. 迈出三步吧。

-- Step 1: Give the males a temporary gender value (gender X) - 第1步:给男性一个临时的性别价值(性别X)

-- Step 2: Set the female records to male (F to M) - 步骤2:将女性记录设置为男性(F到M)

-- Step 3: Set the old male records to female ( X to F) - 步骤3:将旧的男性记录设置为女性( X到F)

Update table Set Gender = 'X' where Gender = 'M'
Update table Set Gender = 'M' where Gender = 'F'
Update table Set Gender = 'F' where Gender = 'X'

In MSSQL you could do this: 在MSSQL中你可以这样做:

UPDATE table SET gender = CASE WHEN gender = 'M' THEN 'F' ELSE 'M' END

If there is anything similar is My-SQL then this is one easy statement. 如果有类似的东西是My-SQL,那么这是一个简单的陈述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM