力扣(leetcode)1121和1251题(MySQL)

1121.查询结果的质量和占比

题目链接:1121.查询结果的质量和占比
力扣(leetcode)1121和1251题(MySQL)_第1张图片

解答
# Write your MySQL query statement below
select query_name,
round(avg(rating/position),2) as quality, 
round(100*avg(rating<3),2) as poor_query_percentage 
from Queries group by query_name 
having query_name is not null;

1251.平均售价

题目链接:1251.平均售价
力扣(leetcode)1121和1251题(MySQL)_第2张图片

解答
select p.product_id,
round(ifnull(sum(price*units)/sum(units),0),2) as average_price 
from Prices p left join UnitsSold u on p.product_id = u.product_id 
where u.purchase_date between p.start_date and p.end_date 
or u.product_id is null group by p.product_id;

最后,我写了一篇MySQL教程,里面详细的介绍了MySQL的基本概念以及操作指令等内容,欢迎阅读!
MySQL数据库万字保姆级教程

你可能感兴趣的:(MySQL解题集,leetcode,mysql,算法)