hackerrank之mysql的放弃
1. 用mysql选择奇偶数
获取偶数的方法
select * from pos_info_report_tmp_20110712 r
where mod(r.id,2) = 0;
获取奇数的方法
select * from pos_info_report_tmp_20110712 r
where mod(r.id,2) = 1;
在hackerrank上面的例子
select distinct(city)
from station
where mod(id,2)=0;
2.求两个column的差
题目为:
select count(city) - count(distinct(city))
from station;
3.Weather Observation Station 5
select CITY,LENGTH(CITY) from STATION order by Length(CITY) asc, CITY limit 1;
select CITY,LENGTH(CITY) from STATION order by Length(CITY) desc, CITY limit 1;
4.Weather Observation Station 6
查找以元音字母开头的城市名称
具体笔记在mysql中的60. 牵扯到了正则表达式
select distinct city
from station
where city regexp '^[aeiou]';
5.Weather Observation Station 7
与第4题类似,求以元音字母结尾的城市
select distinct city
from station
where city regexp '[aeiou]$';
6.Weather Observation Station 8
同类型题,查找以元音字母开头和结尾的城市名称
select distinct city
from station
where city regexp '^[aeiou]'
and city regexp '[aeiou]$';
感觉还可以优化一下。。。
SELECT DISTINCT city FROM station WHERE city RLIKE '^[aeiou].*[aeiou]$';
在讨论区看见的解法,使用RLIKE
7.Weather Observation Station 9
同类型题,查找城市名字不以元音字母开头的
select distinct(city)
from station
where city regexp '^[^aeiou]';
8.Weather Observation Station 10
查找不以元音字母结尾的城市名称
select distinct city
from station
where city not regexp '[aeiou]$';
not regexp(): Negation of REGEXP
9.Weather Observation Station 11
查找不以元音字母结尾或者开头的城市
SELECT DISTINCT CITY
FROM STATION
WHERE city regexp '^[^aeiou]'
OR city not regexp '[aeiou]$';
10.Weather Observation Station 12
查找不以元音字母结尾和者开头的城市
SELECT DISTINCT CITY
FROM STATION
WHERE city regexp '^[^aeiou]'
and city not regexp '[aeiou]$';
11.Higher Than 75 Marks
select name
from students
where marks >75
order by right(name,3),id asc;
12.Type of Triangle
Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:
定义三角形类型
SELECT CASE
WHEN A+B>C AND B+C>A AND A+C>B THEN
CASE
WHEN A=B AND B=C THEN 'Equilateral'
WHEN A=B or A=C OR B=C THEN 'Isosceles'
ELSE 'Scalene'
END
ELSE 'Not A Triangle'
END
FROM TRIANGLES ;
select case
when A+B <= C or A+C <= B or B+C <= A then "Not A Triangle"
when A=B and B=C then "Equilateral"
when A=B or A=C or B=C then "Isosceles"
else "Scalene"
end
from TRIANGLES;
ps: 答案来自stackoverflow, 要先排除不是三角形的,再去定义三角形类型?
case的逻辑是一层接着一层的,不是并列同时发生的
13.Revising Aggregations - The Count Function
select count(id)
from city
where population >100000;
14.Revising Aggregations - The Sum Function
select sum(population)
from city
where district = 'california';
15.Revising Aggregations - Averages
select avg(population)
from city
where district ='california';
16.Average Population
select round(avg(population),0)
from city;
或者可以尝试用ceil()
17.Japan Population
select sum(population)
from city
where countrycode = 'JPN';
18.Population Density Difference
select max(population)- min(population) as diff
from city;
19. The Blunder
SELECT CEIL(AVG(Salary)-AVG(REPLACE(Salary,'0',''))) FROM EMPLOYEES;
答案来自讨论区,第一开始想用trim,但是trim只可以删除字符串前后的0,像是2006 这种金额,中间有0,就没有办法删除。需要用replace
https://www.jianshu.com/p/5ec5e7278110
https://dev.mysql.com/doc/refman/8.0/en/regexp.html
https://stackoverflow.com/questions/38561938/type-of-triangle-in-mysql