本文主要梳理《对比Excel,轻松学习SQL数据分析》对于自己有用的sql知识。这本书中的所有代码和函数均适用于my sql 8.0版本。
SQL的基本功能 数据定义(DDL)--对数据库,表,视图,索引等对象进行的创建,删除,修改的操作。 数据操作(DML)--对表进行的增,删,查,改操作。 数据控制(DCL)--赋予或取消某个用户对表或列的DML权限。 SQL查询的处理步骤
- 查询分析分析sql语句的词法或语法是否正确。
- 查询检查检查sql语句中的数据库,表名,列名是否存在。
- 查询优化根据具体情况,选择效率最高的执行策略。
- 查询执行执行查询语句。
查询数据库信息
use information_schema;-- schemata表存储了my sql所有与库相关的信息select * from information_schema.`SCHEMATA`;
查询结果:
![60dc47882c6106d6e7814bef6e45ec3a.png](https://img-blog.csdnimg.cn/img_convert/60dc47882c6106d6e7814bef6e45ec3a.png)
查询数据表信息
select* from information_schema.`TABLES`
查询结果:
![9dcf9bdfcd20fc09d38796f05576b333.png](https://img-blog.csdnimg.cn/img_convert/9dcf9bdfcd20fc09d38796f05576b333.png)
查询列信息
select * from information_schema.`COLUMNS`
查询结果:
![7477d2769379b4f418cce666ded36ce6.png](https://img-blog.csdnimg.cn/img_convert/7477d2769379b4f418cce666ded36ce6.png)
select * from student;
原表:
![a390169e986a4984ec21e1e4bb7e0f88.png](https://img-blog.csdnimg.cn/img_convert/a390169e986a4984ec21e1e4bb7e0f88.png)
select * from student limit 3,2;
取student表第3行(不包含第3行)之后的2行数据:
![813cc5ddc64456474cd7eb6d1f568a37.png](https://img-blog.csdnimg.cn/img_convert/813cc5ddc64456474cd7eb6d1f568a37.png)
- 过滤掉缺失值
-- 缺失值为空值或空格时select * from student where class!="" or class!=" "-- 缺失值为null时select * from student where class is not null
2.填充缺失值
ifnull(列名,填充值);
coalesce(列名,填充名)
填充null值
select * ,ifnull(sbirthday,'if缺失值') ,coalesce(sbirthday,'coal缺失值')from student where sbirthday is null
运行结果:
![19a357e0f751d4667926691d95968c1f.png](https://img-blog.csdnimg.cn/img_convert/19a357e0f751d4667926691d95968c1f.png)
select * ,cast(sno as char(10)) as cast_type ,convert(sno,char(10)) as convert_typefrom student
![3b9f197b2814e092f0025ec6b8b1146b.png](https://img-blog.csdnimg.cn/img_convert/3b9f197b2814e092f0025ec6b8b1146b.png)
select *,replace(ssex,'male','男') as male,replace(ssex,'female','女') as femalefrom student
合并--concat(A,a),表示将A列和a列合并
concat_ws(符号c,A,a),表示将A列与a列以符号c连接
select *,concat(sname,ssex),concat_ws('+',sname,ssex) from student
![5e4dce055eaaa4e4e833950578bf64ec.png](https://img-blog.csdnimg.cn/img_convert/5e4dce055eaaa4e4e833950578bf64ec.png)
select char_length("自学sql"),length("自学sql")
![4b348c4259bac70bcc18c2784f14012f.png](https://img-blog.csdnimg.cn/img_convert/4b348c4259bac70bcc18c2784f14012f.png)
select repeat("study sql",3)
![a3599a5ddeed7a4e432c8291613a3505.png](https://img-blog.csdnimg.cn/img_convert/a3599a5ddeed7a4e432c8291613a3505.png)
select * ,case classwhen "class2" then "2班" when "class3" then "3班" when "class4" then "4班" when "class5" then "5班" else "" end as class_1from student
![03da6bbcb1a90258ade5dfe83292f04b.png](https://img-blog.csdnimg.cn/img_convert/03da6bbcb1a90258ade5dfe83292f04b.png)
select * ,casewhen `degree`>=60 then "及格" when `degree`<60 then "不及格" else "" end as scorefrom practice.score
![c60941c894c4826ad98586697e10976d.png](https://img-blog.csdnimg.cn/img_convert/c60941c894c4826ad98586697e10976d.png)
select * ,date_format(sbirthday,"%d") as date1 ,extract(day from sbirthday) as date2from student
![6ba01ccb99f23f1c32ac0ac6fa44f2a8.png](https://img-blog.csdnimg.cn/img_convert/6ba01ccb99f23f1c32ac0ac6fa44f2a8.png)
select count(salesno) ,count(if(DATE_FORMAT(convert(dimDateID,datetime),"%y-%m-%d")="17-06-06",salesNo,null)) as last_dayfrom dw.fct_sales
![eb506444b08f3e6bd6ebfe1a96960d05.png](https://img-blog.csdnimg.cn/img_convert/eb506444b08f3e6bd6ebfe1a96960d05.png)