using等价于join操作中的on

使用using必须满足如下两个条件:

1. 查询必须是等值连接。
2. 等值连接中的列必须具有相同的名称和数据类型。

【举例】:此为牛客网SQL进阶第14题

表examination_info(exam_id试卷ID, tag试卷类别, difficulty试卷难度, duration考试时长, release_time发布时间)如下:
在这里插入图片描述
表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分)如下:
在这里插入图片描述
【题目】:从exam_record数据表中计算所有用户完成SQL类别高难度试卷得分的截断平均值(去掉一个最大值和一个最小值后的平均值)

【思路】

round(…,1)保留一位小数;

计算截断平均值:(完成SQL类别高难度试卷的总分 - 最大值 - 最小值) / (总个数-2): (sum(score) - max(score) - min(score)) / (count(score) - 2)

select tag,difficulty,
round((sum(score) - max(score) - min(score)) / (count(score) - 2 ), 1)
     as clip_avg_score
     from exam_record
     join examination_info using(exam_id)
     where tag = "SQL" and difficulty = "hard";
select tag,difficulty,round((sum(score)-max(score)-min(score))/(count(score)-2),1)
as clip_avg_score
from examination_info ei
join exam_record er
on ei.exam_id=er.exam_id
where  tag='SQL' and difficulty='hard';

【结果】

tagdifficultyclip_avg_score
SQLhard81.7
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐