第一周-Mysql学习总结

一、安装Mysql导入CSV表格

1.1 load data local infile 'C:/Users/Lan/Desktop/user_info_utf.csv' into table userinfo fields terminated by ',';

注意: 要有fields terminated by ',',' 是因为csv 文件是以逗号为分割符的

出现的错误:

ERROR 1148 (42000): The used command is not allowed with this MySQL version

网上查了下,是因为local_infile这个功能默认是禁止(local_infile=0,启动的话要改为1)的,需要把这个功能打开,网上有很多方法,这边说一下我能成功导入的方法:

第一步在my.ini文件中添加语句loose-local-infile=1;

第二步运行的时候设置全局变量 set global local_infile=1。

参考:https://www.jianshu.com/p/413719cfdeb1

1.2 把时间格式标准化变成 2019-02-27

错误:update orderinfo set paidtime=replace(paidtime,'/','-') where paidtime is not null Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect. 0.000 sec

在增删改之前加上 set sql_safe_updates=0;

二、更新和删除表数据、表结构

# 清除表全部数据命令

truncate userinfo;(速度更快,删除后重建)

#删除数据

delete from table where…;

#更新数据

update table orderinfo set…where…

–绝对不要轻易忽略where字句,否则会更新或删除全部的行

–在使用where条件前先使用select进行测试,保证正确无误,因为Mysql是没有撤销功能的


#新建表

create table t1

(id   int   not null  

………………

)engine=innodb;

# 删除表命令

drop table user info;

# 修改表字段类型命令

alter table orderinfo modify column paidtime

varchar(45);

三、case、if分条件,自定义函数用于计数

3.1 case when…then…when…then…end 

case when 用在select 语句中,新的字段new_column_name可以用来排序,end 后面加as new_colume_name

按各科成绩进行排序,并显示排名, Score 重复时合并名次
select sc.CId , case when @fontscore=score then @curRank when @fontscore:=score then @curRank:=@curRank+1 end as rank,sc.score

from (select @curRank:=0 ,@fontage:=null) as t ,sc----当做初始化

ORDER BY sc.score desc

当满足某一条件时,执行某一result


当colume 与condition 条件相等时结果为result

例如男女交换性别


3.2 if进行分组计数

@rank:=if(@gen=gender,@rank+1,1) rank

@gen:=gender

(select @rank:=0,@gen:=null) temp

如果相等则rank+1,接着@gen会被赋值该行的值,直到遇到不等的rank重新变为1

四、group by和MAX()一起用注意问题

关于group by 和max函数一起使用的坑

如果输出的除了分组列和max列还有其他列,其他列会是group by分组后的第一条记录。

五、时间函数

DATE_SUB()、DATE_ADD()可通用,对指定的时间进行增加或减少一个指定的时间间隔,语法如下:

DATE_ADD(date,INTERVAL expr type)

DATE_SUB(date,INTERVAL expr type)

date是指定的日期,INTERVAL为关键词,expr是具体的时间间隔,type是时间单位。expr可以为一个负数

DATEDIFF()日期差

DATEDIFF(date1,date2),可以是负数

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • MYSQL 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 MySQL基本操...
    Kingtester阅读 7,886评论 5 116
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,554评论 0 13
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,266评论 0 7
  • 笔记: 一、聚合函数:计数 最大值 最小值 平均数 求和 1.计数 COUNT() 忽略NULL值 方式1:COU...
    凤之鸠阅读 5,394评论 0 1
  • 有时候,明明很痛苦,却不得不以笑容去对待这个世界,常常给别人带来快乐的人,也许心早已支离破碎,就像一个小丑,总把自...
    肥桦来啦阅读 240评论 0 1