MySQL常用函数记录,参考MySQL函数大全

MySQL的url拼接:jdbc.url=jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8&allowMultiQueries=true,其中针对allowMultiQueries的说明:在sql语句中允许使用“;”来分隔多条查询(真/假,默认值为“假”)。

一、动态创建日志表中的知识点

1、在数据库中查找相应的数据表的方法:
数据库中有个库叫information_schema,其中有个表名称为tables,此表中包含有各个表所属的数据库的名称和表的名称,示例如下

select table_name from information_schema.tables 
where TABLE_SCHEMA='hibernate' and TABLE_NAME like 't_%';

说明:information_schema.tables是指information_schema这个数据库中的tables数据表
     TABLE_SCHEMA是指要查询的要查询的数据库的名称;
     TABLE_NAME是指要查询的数据表的名称;

2、通过表名创建一个同样类型的表,示例如下:

create table if not exists log_2017_05 like log_2017_06;
说明:创建一个新表,构造和log_2017_06相同;

3、利用union字段查询具有相同构造的数据表数据

select name from log_2017_06 union select name from log_2017_05
说明:使用union进行查询时,请保证两个表的结构相同或者类似

4、information_schema这张数据表保存了MySQL服务器所有数据库的信息;如数据库名,数据库的表,表栏的数据类型与访问权限等

SCHEMATA表:提供了关于数据库的信息
TABLES表:给出了关于数据库中的表的信息
COLUMNS表:给出了表中的列信息

5、动态生成日志表的工具类

/**
 * 动态生成日志表的工具类(按照月份生成相应的日志表)
 */
public class LogUtil {

    /**
     * @param offset:当前月份的偏移量,如0代表当前月份,-1代表上个月,1代表下个月;
     * @return :动态生成日志表的名称,按照log_年份_月份的格式;
     */
    public static String generateLogTableNameUtil(int offset){
        Calendar calendar=Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        //月份是从0开始的,故要加1;
        int month=calendar.get(Calendar.MONTH)+1+offset;
        if(month > 12){
            year ++ ;
            month = month - 12 ;
        }
        if(month < 1){
            year -- ;
            month = month + 12 ;
        }
        DecimalFormat df=new DecimalFormat("00");
        return "log_"+year+"_"+df.format(month);
    }
}

二、字符函数

1、length(str) :获取参数值的字节个数
如:select length(“hello”) 返回:5

2、concat(str1,str2,…): 拼接字符串
如: select concat(“hello”,”_”,”world”) 返回:hello_world

3、upper(str) 和lower(str) :字符大小写
如:select upper(“nihao”) 返回:NIHAO
select lower(“NIHAO”) 返回:nihao

前三个函数综合使用:将姓名的姓大写名小写,中间用下划线(_)连接起来
如:select concat(upper(“feng”),”_”,lower(“Lang”)) 返回:FENG_lang

4、substr(str from pos) 和substring(str,pos ) :字符串的切割(注意:MySQL中的索引是从1开始)
说明:若substr(str,pos)中只有一个参数,截取从指定索引到末尾所有字符,如:
select SUBSTR(“The People’s Republic Of China”,5) as name 返回:People’s Republic Of China

说明:若substr(str,from,pos)中有两个参数,截取从指定索引到指定字符长度的所有字符,如:
select SUBSTR(“The People’s Republic Of China”,5,6) as name 返回:People

5、instr(str,substr):返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)
如:select INSTR(‘fenglang’,’lang’) 返回:5

6、trim([[both | leading | trailing] [remstr] from] str):返回前缀或后缀remstr被删除了的字符串str(位置参数默认both,remstr默认值为空格)
说明:默认情况下trim( )函数去除的是空格,如:
select trim(” nihao “) as output 返回:nihao

说明:去除字符串左右两侧特定的字符,效果同select trim(both ‘x’ from “xxxxnixxhaoxxxxxx”) as output,如:
select trim(‘x’ from “xxxxnixxhaoxxxxxx”) as output 返回:nixxhao

说明:去除字符串左侧特定的字符,如:
select trim(leading ‘x’ from “xxxxnixxhaoxxxxxx”) as output 返回:nixxhaoxxxxxx

说明:去除字符串右侧特定的字符,如:
select trim(TRAILING ‘x’ from “xxxxnixxhaoxxxxxx”) as output 返回:xxxxnixxhao

注意:ltrim(str):删除字符串str左侧的空格,rtrim(str):删除字符串str右侧的空格

7、lpad(str,len,padstr):用字符串padstr填补str左端直到字串长度为len并返回
如:select LPAD(‘nihao’,12,’x’) as output 返回:xxxxxxxnihao

rpad(str,len,padstr):用字符串padstr填补str右端直到字串长度为len并返回
如:select rpad(‘nihao’,12,’x’) as output 返回:nihaoxxxxxxx

8、replace(str,from_str,to_str):用字符串to_str替换字符串str中的子串from_str并返回
如:select REPLACE(‘?nihao?world’,’?’,’#’) as output 返回:#nihao#world

三、数学函数

1、round(n,d) :返回n的四舍五入值,保留d位小数(d的默认值为0)
如:select round(1.68) as output 返回:2
select round(1.68,1) as output 返回:1.7

2、ceil(n) :向上取整,返回>=参数的最小整数
如:select ceil(1.23) as output 返回:2

3、floor(n):向下取整,返回<=参数的最大整数
如:select floor(1.23) as output 返回:1

4、truncate(n,d) :保留数字n的d位小数并返回
如: select TRUNCATE(12.8967,2) as output 返回:12.89

5、mod(n,m) :取模运算,返回n被m除的余数(同%操作符)
如:select mod(12,-5) as output 返回:2

6、abs(n) :返回n的绝对值
如:select abs(-5) as output 返回:5

7、pow(x,y) : 返回x的y次幂
如:select pow(2,3) as output 返回:8

8、sqrt(n) :返回非负数n的平方根
如:select sqrt(16) as output 返回:4

9、rand( ):返回0~1之间的随机浮点数
如:select RAND() as output 返回:0~1之间的随机浮点数

10、least(x,y,…) :返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较) ;
greatest(x,y,…) :返回最大值;

如:”select least(12,23,2,-2,39,-34,-62.54) as output 返回:-62.54
select greatest(12,23,2,-2,39,-34,-62.54) as output 返回:39

四、时间日期函数

1、now() :以’yyyy-mm-dd hh:mm:ss’格式返回当前日期
curdate():以’yyyy-mm-dd’格式返回当前日期值
curtime():以’hh:mm:ss’或hhmmss格式返回当前时间值

2、year(date):返回date的年份
month(date):返回date的月份值
dayofmonth(date) :返回date是一月中的第几日(在1到31范围内)
dayofyear(date) :返回date是一年中的第几日(在1到366范围内)
dayname(date) :返回date是星期几(按英文名返回)
quarter(date) :返回date是一年的第几个季度
week(date,first) :返回date是一年的第几周(first默认值0,first取值1表示周一是周的开始,0从周日开始)

3、date_format(date,format):根据format字符串格式化date值
说明:在format字符串中可用标志符如下
%m 月名字(january……december)
 %w 星期名字(sunday……saturday)
 %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
 %Y 年, 数字, 4 位
 %y 年, 数字, 2 位
 %a 缩写的星期名字(sun……sat)
 %d 月份中的天数, 数字(00……31)
 %e 月份中的天数, 数字(0……31)
 %m 月, 数字(01……12)
 %c 月, 数字(1……12)
 %b 缩写的月份名字(jan……dec)
 %j 一年中的天数(001……366)
 %H 小时(00……23)
 %k 小时(0……23)
 %h 小时(01……12)
 %i 小时(01……12)
 %l 小时(1……12)
 %i 分钟, 数字(00……59)
 %r 时间,12 小时(hh:mm:ss [ap]m)
 %t 时间,24 小时(hh:mm:ss)
 %s 秒(00……59)
 %p am或pm
 %w 一个星期中的天数(0=sunday ……6=saturday )
 %u 星期(0……52), 这里星期天是星期的第一天
 %u 星期(0……52), 这里星期一是星期的第一天
 %% 字符%
 如:select DATE_FORMAT(now(),’%Y-%m-%d %H-%i-%s’) 返回:格式化后的日期

4、str_to_date(str,format) :将字符转化为日期
如:select STR_TO_DATE(‘1990-4-4’,’%Y-%m-%d’) 返回:1990-04-04

5、datediff(date_expression_1,date_expression_2):返回两个日期值之间的天数
如:select datediff(‘2018-05-06’,’2017-05-04’) as result 返回:367

五、分组查询

语法如下:
select 分组函数,列(要求出现在group by的后面)
from 表
【where 筛选条件】
group by 分组的列名
having 分组函数
order by 排序
注意:
1、查询列表必须特殊,要求是分组函数和group by 后出现的字段
2、分组查询中的筛选条件分为两类

筛选顺序

数据源

位置

关键字

分组前筛选

原始表

group by子句的前面

where

分组后筛选

分组后的结果集

group by子句的后面

having