数分正式篇之第五节


欢迎来到煊煊周末小课堂!蹭了非常善良的热心学长的网课,八次感谢!让我们一起祝福他飞黄腾达!
这次来到了我们的SQL篇,因为之前也讲过SQL的基础语句,所以这次接着上次的学习

数据导入导出

这里插一句哈,老师推荐了MySQL的MySQL workbench可以用来编辑SQL语句,会比较方便,以及mysql notifier是可以查看mysql的运行状态的,但我感觉其他方式也可以,就没安装,感兴趣的小伙伴可以自行安装。

数据的导出

1.可以在图形界面直接操作,右击到处即可,当然首要第一步是先将数据查询出来。
2.通过SQL语句导出:

#字段之间通过,隔开,记录之间通过换行隔开
select * from 表名 into outfile '路径记得是斜杠' fields terminated by',' lines terminated by '\r\n';

数据的导入

1.还是单击导入数据就行,相信大家都能找到这个功能
2.通过SQL语句导入数据:

load data infile '路径' into table 表名 fileds terminated by'\t' lines terminated by '\r\n'; 

如果导入导出出现问题的话,一个可能的解决方案是修改MySQL的my.ini配置文件把secure_file_priu=’ '的上一句注释掉,然后把引号里的内容改为空。

数据操作

排序order by

#将学生表按学生生日排序,默认升序
select * from student order by birthdate ;
#降序desc
select * from student order by birthdate desc;

分组group by

#将学生按性别分组,并显示不同性别的人数
select gender,count(*) from student group by gender

空值 null/not null

#显示表中城市字段不为空的记录
select * from student where city is not null

统计函数

#查询学生数学成绩最高分以及平均分
select max(math) as math_max,avg(math) as math_avg from student

表的连接

内连接

获取两个表中字段匹配的记录,inner join

#两个表中共有的字段前面就要加前缀
select stuinfo.stuid,stuinfo.stuname,gender,math,english from stuinfo inner join stuscore where stuinfo.stuid=stuscore.stuid;

左连接

获取左表中的所有记录,右表中没有对应匹配的就为空

#以left join左边的那个表为基准
select stuinfo.stuid,stuinfo.stuname,gender,math,english from stuinfo left join stuscore where stuinfo.stuid=stuscore.stuid;

右连接

获取右表中的所有记录,左表中没有对应匹配的就为空

#以right join右边的那个表为基准
select stuinfo.stuid,stuinfo.stuname,gender,math,english from stuinfo right join stuscore where stuinfo.stuid=stuscore.stuid;

其他常用操作符

#like,查找城市以Bei开头的记录
select * from stuinfo where city like 'Bei%';
#in,not in
select * from stuinfo where city in ('BJ','SH');
#date_format:日期格式化输出
#将日期转换成年份
select stuname,date_format(birthdate,'%Y') from stuinfo;
#将日期转换成年月
select stuname,date_format(birthdate,'%Y-%m') from stuinfo;
#between区间
select * from stuinfo where birthdate between '1989-1-1' and '1990-1-1';
#distinct去重
select count(distinct city) from stuinfo;
#having经常和统计函数连用
#查询数学平均分>90的班级,并显示出来平均分
select class,avg(math) from stuscore group by class having avg(nath)>90;
#union:合并结果集
select stunmae from stuinfo union select stuname from stuscore
#case:表达式通常与group by连用
#显示不同地区的城市个数
select case city
			when 'BeiJing' then '华北'
			when 'TianJin' then '华北'
			when 'ShangHai' then '华东'
			else '其他' end
as district,count(stuid) from stuinfo group by city
			when 'BeiJing' then '华北'
			when 'TianJin' then '华北'
			when 'ShangHai' then '华东'
			else '其他' end;

索引

速度快,如果表中查询的列有索引,就可以直接去一个位置搜索,而不必查看所有数据。

select index from orders;
#创建索引
alter table orders add index oid_index(oid);
#删除索引
drop index oid_index(oid) on orders;

函数

#先声明下面的内容以//作为SQL语句的结尾
delimiter //
create function myfunction(x int)
returns int
begin 
declare y int;
set y=2*x+1;
return y;
end//
delimiter ;
select myfunction(5);
drop function myfunction(5);

存储过程

单独存储在系统中的,直接引用执行即可

delimter //
create procedure info(score int)
begin
select * from stuscore where math>score;
end//
delimter ;
#调用存储过程
call info(90);
#删除存储过程
drop procedure info;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值