MySql*第二部分(表的增删改查, 常用函数, 字符串函数, 数学函数, 日期函数, 条件函数)

表的基本操作:增,删,改,查

数据添加insert into 表 (字段1,2,3...) value(值1,2,3...)
or: insert into 表 value(值1,2,3...)  /* value添加需要和表字段一一对应 */
or(赋值插入):insert intoset 字段1=值1,字段2=值2...;
or(多条插入):insert into 表(字段1,2,3...) value(值1,2,3),(值1,2,3)...;

数据更新(修改): updateset 字段=值,字段2=值2... where 条件(=,<,>,<>,!=,like,等)符合的条件;#未加where则全部修改。

数据查询:select * from 表  (简单,不作解释)

数据删除:delete fromwhere 条件=满足的条件;
or: delete from student;  #表内数据全部清空!新添加主键自增为删除前最大值+1or: truncate table 表; 表记录全清除!主键自增重置,从1开始!

  /* 使用delete语句时,每删除一条,会在日志中留下记录,而使用truncate时,不会记录在日志中。
    因此truncate语句的执行效率比delete语句要高.
    */
    
简单查询基本语法:
  select [distinct]*|{字段1,23...} 
    from[where 条件表达式1]
    [group by 字段名 [having 条件表达式2]]
    [order by 字段名 [asc|desc]]
    [limit[offset] 记录条数]
    /* []:为可选条件; distinct:单字段去重复,多字段则是整体一致作为重复条件.
        group by 分组,having 对分组的数据再次操作, 
        order by 排序 (null默认为最小值),limit 提取查询到的n条数据,offset 为偏移变量,默认为0(查询结果的下一条开始)
    */
    
条件式:
  条件是否包含👉in / [not] in: select 字段1,2,3 fromwhere 字段 [not] in (数值1,2,3...);
  条件在值a到值b之间👉[not] between and: select 字段1,2,3 fromwhere 字段 [not] between 值1 and 值2;
  条件是否为null👉is [not] null: select 字段1,2,3 fromwhere 条件 is [not] null;
  条件匹配字符串👉[not] like [_/%/\]: select 字段1,2,3 fromwhere [not] like '[_%条件_%]' 
    /* _:通配符,占位符.
        %:任意长度,包括空格.
        \:反转译,使匹配符号为真实符号.数据中本来就有的_%\等.
    */
  多条件和或者👉 and / or :太简单,不作解释!where 条件1 and/or 条件2;
  /* and 和 or 一起使用时,and的优先级高于or! */

 


常用聚合函数

count(字段):计数
sum(字段):求和
avg(字段):求平均值
max,min(字段):求最大值、最小值

查询语句书写顺序:select - from - where - group by - having -order by - limit 
查询语句执行顺序:from - where - group by -having - select -order by - limit

 


字符串类函数

concat(s1,s2,...):拼接字符串,select name,concat(grade,'') from student;select concat('a','b');
length(string):返回字符串字节长度,select length('i love you');  #计算该字符串字节的长度 中文字节长度为2(mysql),自己试下就知道了
char_length(string):返回字符的个数,select char_length('a b c d');  #计算该字符串的长度,字母空格中文等都为1
截取字符串: 
    left(str,截取长度):截取字符串左边,左边开始+长度
    rigth(str,len):截取字符右边,右边开始+长度
    substring(str,起始位置,截取长度):指定位置截取
去除空格:
    ltrim(str):删除字符串前端空格并返回   #不包含中间夹有的空格
    rtrim(str):删除字符串后段空格并返回   #不包含中间夹有的空格
    trim(方向 指定字符 from str/字段):返回从方向端删除指定字符后的字符串
        both:两侧,leading:左,trailing:右;   #只输入str则删除该字符串两侧并返回
字符串替换:
    replace(str,需替换字符串,待替换字符串):select replace('abc','b','d');输出adc;
    insert(str1,x,len,str2):从字符串中的x+len(长度),也就是x开始包含并往后len的内容,替换成str2
       例如: select insert('12345','2',2,'z'); 输出'1z45'; 也就是2开始,23替换成z.
    lower(str):转小写
    upper(str):转大写
reverse(str):字符串内容反转,‘123 5’变‘5 321’;
locate(x,str):返回x在str中的位置,从前面开始,只返回一个位置;

 


数学类函数

abs(n):求绝对值,-30得30;
mod(m,n):求m%n得余数,3/2得1;
floor(n):向下取整数;   #值带小数,返回整数,不四舍五入,22.99,22.01返回22;
ceiling(n):向上取整数;   #值带小数,返回+1的整数,22.01返回23;
round(n):返回四舍五入的值,整数;
round(n,d):d 小数点保留位数,四舍五入后保留,d为负数则该值四舍五入返回整数.如268.22,-1 则8入68为70,输出270;
pow(x,y):求x的y次方,2,3. 2的3次方(幂)为8;

exp(x):返回e的x次方直;  
log(x):返回x相对于基数e对数;
log10(x):返回x相对于基数10的对数; 
pi():返回圆周率;
rand():返回一个0.0-1.0直接的随机数; #试了一下随机数长度达到19.
rand(x):指定一个整数x,则被用作种子值,用来随机产生重现(xian)随机值; #多次运行rand(x),一直重复,直到替换x.
其他三角函数:sin() cos() tan() cot()等;

 


日期类函数

获取当前日期[yyyy-mm-dd]:current_date();
获取当前时间[hh-mm-ss]:current_time();
获取当前日期时间[yyyy-mm-dd hh-mm-ss]:now();
其他:year|quarter 季度|month 月份|day|hour 小时|minute 分钟|second 秒;

weekofyear(x):返回日期x在一年中的第几周;
dayofyear(x):返回日期x是一年中的第几天;

time_to_sec(str):将时间str时间部分转为秒数;
sec_to_time(int):将秒数int转为时间,格式为[hh:mm:ss];
datediff(x2,x1):计算x1到x2之间的日期天数;

adddate(x,interval n type):给时间x加上指定时间n,type包括:[year,month,day,hour,minute,second];
subdate(x,interval n type):给时间x减去指定时间n,type同上; #用法 subdate('2020-03-23',interval 21 day); 
addtime(x,str):给时间x的时间部分加上指定时间str;  # str参数整数为秒,str则需要是['hh:mm:ss']格式 #
subtime(x,str):给时间x的时间部分减去指定时间str;

时间和日期格式化:
date_format(date,format):根据format指定的格式显示date值;
/*
    format参数:
        %Y 年份 返回4位整数,
        %y 年份 返回2位整数,
        %m 月份 返回0~12的整数,
        %d 日期 返回0~31之间的整数,
        %H 小时 (00~23),
        %h 小时 (01~12),
        %i 分钟 (00~59),
        %s 秒   (00~59)
        
        示例:select date_format('2020-03-23','%Y年%m月%d日');  →2020年03月23日.
             select date_format(current_date(),'%y年%m月%d日');  →20年03月23日.
*/

 


条件类函数

if(boolean,v1,v2):如果布尔值为True,返回v1,否则返回v2;
ifnull(v1,v2):如果v1不是null,返回v1,否则返回v2;
case value when v1 then r1 [when v2 then r2][else m] end:如果value为v1就返回r1,如果所有值都不相等,则返回else后面的rn;
case演示: select 字段1 , case 字段2 when 值1 then str1 when 值2 then str2 else str3 end from 表;
        字段2等于值1,就输出str1
        字段2等于值2,就输出str2
        否则输出str3.

 

posted @ 2020-03-23 23:07  逍遥大帝  阅读(231)  评论(0编辑  收藏  举报