题解 | 推荐内容准确的用户平均评分
推荐内容准确的用户平均评分
https://www.nowcoder.com/practice/2dcac73b647247f0aef0b261ed76b47e
# 在if语句中比较属于不同表的两个字段时需要加上表前缀 # if(a.rec_info_l=b.hobby_l,1,0) # distinct表示对后面的一个或多个列的拼接的一整条记录取不重复的结果 # where子句中没法使用查询列的别名进行过滤,因为在调用where子句时,select子句还没开始执行,但是order by能够识别别名,因为order by子句执行顺序在select之后。 # 答案一 # select avg(score) avg_score # from ( # select distinct user_id, score, if(a.rec_info_l=b.hobby_l,1,0) as accuracy # from recommend_tb a # inner join user_action_tb b # on a.rec_user = b.user_id # ) c # where accuracy = 1; # 答案二 select avg(score) avg_score from ( select distinct user_id, score from recommend_tb a inner join user_action_tb b on a.rec_user = b.user_id where hobby_l = rec_info_l ) a
在if语句中比较属于不同表的两个字段时需要加上表前缀
if(a.rec_info_l=b.hobby_l,1,0)
distinct表示对后面的一个或多个列的拼接的一整条记录取不重复的结果
where子句中没法使用查询列的别名进行过滤,因为在调用where子句时,select子句还没开始执行,但是order by能够识别别名,因为order by子句执行顺序在select之后。