在刷LeetCode的时候遇到的SQL题目
627.Swap Salary
Given a table salary
, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate temp table.
For example:
| id | name | sex | salary |
|----|------|-----|--------|
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |
After running your query, the above salary table should have the following rows:
| id | name | sex | salary |
|----|------|-----|--------|
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |
大概就是要求交换表中sex列的性别
一开始想着用update salary set sex = f when sex = m再用个AND,后来一想这样执行的结果应该是错的、
然后就偷看了solution,才知道还有when case语句,所以记录一下
update salary set sex =
case sex
when 'm' then 'f'
else 'm'
end;
大概意思就是when m,then就设置为f,else都设置为m
最新评论
这可是个好东西啊,网站收藏了,抽空搞个VPS
那个国家选错了可以更改吗?
设备限制支持5个设备,我5个设备已经用满了,并且有一个设备不用了,怎么更换设备呢,怎么将不用的设备解绑呢?
还是不行,用了一天443端口就被墙了,防火墙似乎对这个端口特别敏感
服了 刚好是华为用户 请问该怎么办?
楼主你好,我安装了解锁ChatGpt的一键脚本,可是我想卸载,不知道怎么卸载