case when then的用法-leetcode交换工资

本文介绍SQL中case函数的两种格式:简单case函数和case搜索函数,并通过实例展示了如何使用更新查询交换数据库表中性别字段的值,无需使用中间临时表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

case具有两种格式:简单case函数和case搜索函数。

--简单case函数
case sex
  when '1' then ''
  when '2' then '女’
  else '其他' end
--case搜索函数
case when sex = '1' then ''
     when sex = '2' then ''
     else '其他' end

 

leetcode交换工资:

 

给定一个 salary表,如下所示,有m=男性 和 f=女性的值 。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求使用一个更新查询,并且没有中间临时表。

 

例如:

 

| id | name | sex | salary |
|----|------|-----|--------|
| 1  | A    | m   | 2500   |
| 2  | B    | f   | 1500   |
| 3  | C    | m   | 5500   |
| 4  | D    | f   | 500    |

 

运行你所编写的查询语句之后,将会得到以下表:

 

| id | name | sex | salary |
|----|------|-----|--------|
| 1  | A    | f   | 2500   |
| 2  | B    | m   | 1500   |
| 3  | C    | f   | 5500   |
| 4  | D    | m   | 500    |


SQL语句:

update salary set sex=case sex when 'm' then 'f' else 'm' end

 

转载于:https://www.cnblogs.com/ConnorShip/p/10170633.html

### LeetCode C++ Problems and Solutions #### Square Root Calculation Implementation For calculating the square root of a number `x` on LeetCode using C++, one approach involves utilizing mathematical functions such as logarithms and exponentials. The implementation checks if `x` is zero to handle edge cases directly returning 0 when true. For other values, it calculates an approximate integer value by taking half the natural logarithm of `x`, then applying exponential function[^1]. Verification ensures that `(ans + 1)` squared does not exceed `x`; otherwise, returns `ans`. ```cpp class Solution { public: int mySqrt(int x) { if (x == 0) { return 0; } int ans = exp(0.5 * log(x)); return ((long long)(ans + 1) * (ans + 1) <= x ? ans + 1 : ans); } }; ``` #### Array Rotation Methodology Regarding array rotation within LeetCode's problem set implemented in C++, this solution rotates elements efficiently without additional space complexity beyond O(1). By reversing parts of the vector after computing effective rotations needed (`k % size`), three reversals achieve desired outcome: full reversal followed by segment-specific ones up until index `k` and from `k` onwards respectively[^2]. ```cpp #include <iostream> #include <vector> using namespace std; class Solution { public: void rotate(vector<int>& nums, int k) { int n = nums.size(); k %= n; reverse(nums.begin(), nums.end()); reverse(nums.begin(), nums.begin() + k); reverse(nums.begin() + k, nums.end()); } }; // Note: Test case verification code omitted for brevity. ``` #### Daily Temperatures Problem Approach In solving the "Daily Temperatures" challenge presented at LeetCode with C language adaptation available but focusing here conceptually similar methods apply including stack-based algorithms or optimized iteration techniques aimed at minimizing node traversals while maintaining efficiency standards expected during interviews preparation phase[^3].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值