小知识点
取余数:mod(数,2)
取第一个字母:left(name,1) 或者name like ‘M%’
196. 删除重复的电子邮箱
题型:删除列A中重复的记录,指保留列B最小的那一行记录
答案:delete t1 from 表名 t1,表名 t2 where t1.列A=t2.列A and t1.列B<t2.列B
DELETE p1
from Person p1 , Person p2
where p1. email=p2.email and p1.id>p2.id
删除语句: DELETE 表 from表 where 条件
627. 变更性别
题型:列A满足条件1时,列B的记录改为记录1,否则为记录2
解答:update 表名 set 列B= case 列A when 条件1 then 记录1 else 记录2 end;
UPDATE salary
SET
sex = CASE sex
WHEN 'm' THEN 'f'
ELSE 'm'
END;
更新语句:UPDATE 表名 SET 更改内容
1070. 产品销售分析 III
题型:根据列A分组,取各组中列B记录最小的那一行
解答1:select * from 表名 where (列A,列B) in (select 列A,min(列B) from 表名 group by 列A)
解答2:窗口函数,先排序,再删选rank=1的列
select * from (select *,rank()over(partition by 列A order by 列B) as rr from 表名) as t
where rr=1
select product_id,year as first_year,quantity,price
from Sales
where (product_id,year) in
(select product_id,min(year) from Sales group by product_id)
select product_id,year as first_year,quantity,price
from (select *,rank()over(partition by product_id order by year ) as rr from Sales
) as t
where rr=1
534. 游戏玩法分析 III
题型:根据ID列A分组,然后对于日期列B,进行列C值的累计求和
解答:select *, sum(列c值)over(partition by 列A order by 列B) from table
select player_id,event_date,
sum(games_played)over(partition by player_id order by event_date asc) as games_played_so_far
from Activity
550. 游戏玩法分析 IV
题型:计算比率–count(distinct 列A)
读题一定要仔细,这里不是连续两日登录,是首次登录后的第二天
函数:data()、ifnull(值,0)、round(值,2)、
select
ifnull(
round(
(count(distinct player_id))/(select count(distinct player_id) from activity)
,2)
,0) as fraction
from activity
where (player_id,event_date) in
(
select player_id,date(min(event_date)+1) from activity group by player_id
)