MySQL 学习笔记(一)

MySQL 学习笔记(一)
学习内容:金老师SQL
SQl必须要记住的法则一:col (列 columns)代表属性
法则二: select col,col… from … where …

eg:按角色分组算出每个角色按有办公室和没办公室的统计人数(列出角色,数量,有无办公室,注意一个角色如果部分有办公室,部分没有需分开统计)

SELECT role,COUNT(*),building is not null FROM employees GROUP BY ROLE,building is null;

相同答案

SELECT Role,count(*) as count,building is not null as bn FROM employees group by Role,bn

eg:按导演分组计算销售总额,求出平均销售额冠军(统计结果过滤掉只有单部电影的导演,列出导演名,总销量,电影数量,平均销量)

SELECT director,SUM(domestic_sales+international_sales) as sale,COUNT(*) as num,round(SUM(domestic_sales+international_sales))/COUNT(*) as avg_sale FROM movies inner join boxoffice on movies.id=boxoffice.movie_id GROUP BY director HAVING COUNT(*)>1 ORDER BY avg_sale DESC LIMIT 1
SELECT director,SUM(domestic_sales+international_sales) as sale,COUNT(*) as num,max(round(SUM(domestic_sales+international_sales))/COUNT(*)) as avg_sale FROM movies inner join boxoffice on movies.id=boxoffice.movie_id GROUP BY director HAVING COUNT(*)>1

SQL整体流程
MySQL 学习笔记(一)_第1张图片

(注意onenote学习内容粘贴过来要用ctrl+shift+v)

你可能感兴趣的:(MySQL,mysql,数据库,数据分析)