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.
| 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 |
update salary
set sex=case
when sex='m' then 'f'
else 'm'
end
Triangle Judgement
A pupil Tim gets homework to identify whether three line segments could possibly form a triangle.
However, this assignment is very heavy because there are hundreds of records to calculate.Could you help Tim by writing a query to judge whether these three sides can form a triangle, assuming table
triangle
holds the length of the three sides x, y and z.
| x | y | z | |----|----|----| | 13 | 15 | 30 | | 10 | 20 | 15 |For the sample data above, your query should return the follow result:
| x | y | z | triangle | |----|----|----|----------| | 13 | 15 | 30 | No | | 10 | 20 | 15 | Yes |
select x,y,z,
case
when x+y>z and x+z>y and y+z>x then 'Yes' else 'No'
end as triangle
from triangle
方法二
select x,y,z,
if (x+y>z and x+z>y and y+z>x,'Yes', 'No') as triangle
from triangle