MySQL-操作符及常用函数
一、操作符优先级
展示了所有操作符的执行优先级,从高到低,同一行中的操作符优先级相同,相同优先级的情况下则从左到右执行
如果想改变优先级执行顺序,则可以使用括号:
mysql> SELECT 1+2*3; #7 mysql> SELECT (1+2)*3;#9
二、对比操作符
#对比操作符的执行结果为true,false,null三种 Between A and B #代表检查值是否在A和B之间 Coalesce() #代表返回第一个非Null的值 = #代表相等操作符 > #代表大于操作符 >= #代表大于等于操作符 Greatest() #代表返回最大的值 In() #代表检查值是否在一系列的值之中 Interval() #代表返回比第一个参数小的参数的位置 is/is not #代表检查值是否与布尔值相同/不同 Is not null #代表检查值是否是非NULL Is null #代表检查值是否是NULL Isnull() #代表检查参数是NULL Least() #代表返回最小的参数 < #代表小于操作符 <= #代表小于等于操作符 Like #代表字符匹配 Not between A and B #代表检查值是否不在A和B的范围之内 !=/<> #代表不等于操作符 Not in() #代表检查值是否不在一系列值的当中 Not like #代表检查值是否不匹配 Strcmp() #对比两个字符串 ----------------------------------------------------------------------------------------- #=号对比操作符 mysql> SELECT 1 = 0; #0 mysql> SELECT '0' = 0; #1 mysql> SELECT '0.0' = 0; #1 mysql> SELECT '0.01' = 0; #0 mysql> SELECT '.01' = 0.01; #1 #<>/!=号对比操作符 mysql> SELECT '.01' <> '0.01'; #1 mysql> SELECT .01 <> '0.01'; #0 mysql> SELECT 'zapp' <> 'zappp'; #1 #<=小于等于对比操作符 mysql> SELECT 0.1 <= 2; #1 #<小于对比操作符 mysql> SELECT 2 < 2; #0 #>=大于等于对比操作符 mysql> SELECT 2 >= 2; #1 #>大于对比操作符 mysql> SELECT 2 > 2; #0 #is操作符 mysql> SELECT 1 IS TRUE, 0 IS FALSE, NULL IS UNKNOWN; #1, 1, 1 #Is not操作符 mysql> SELECT 1 IS NOT UNKNOWN, 0 IS NOT UNKNOWN, NULL IS NOT UNKNOWN; #1, 1, 0 #Is null对比操作符 mysql> SELECT 1 IS NULL, 0 IS NULL, NULL IS NULL; #0, 0, 1 #Is not null对比操作符 mysql> SELECT 1 IS NOT NULL, 0 IS NOT NULL, NULL IS NOT NULL; #1, 1, 0 #Expr between min and max对比操作符 #相当于min <= expr AND expr <= max mysql> SELECT 2 BETWEEN 1 AND 3, 2 BETWEEN 3 and 1; #1, 0 mysql> SELECT 1 BETWEEN 2 AND 3; #0 mysql> SELECT 'b' BETWEEN 'a' AND 'c'; #1 mysql> SELECT 2 BETWEEN 2 AND '3'; #1 mysql> SELECT 2 BETWEEN 2 AND 'x-3'; #0 #expr NOT BETWEEN min AND max #相当于NOT (expr BETWEEN min AND max) #COALESCE(value,...)对比操作符 #返回第一个非NULL的值,如果没有非null值,则返回NULL mysql> SELECT COALESCE(NULL,2); #2 mysql> SELECT COALESCE(NULL,NULL,NULL); #NULL #GREATEST(value1,value2,...) #返回其中最大的值 mysql> SELECT GREATEST(2,0); #2 mysql> SELECT GREATEST(34.0,3.0,5.0,767.0); #767.0 mysql> SELECT GREATEST('B','A','C'); #'C' #expr IN (value,...)对比操作符 #当expr值能在values中找到,则返回1,否则返回0 mysql> SELECT 2 IN (0,3,5,7); #0 mysql> SELECT 'wefwf' IN ('wee','wefwf','weg'); #1 mysql> SELECT (3,4) IN ((1,2), (3,4)); #1 mysql> SELECT (3,4) IN ((1,2), (3,5)); #0 #expr NOT IN (value,...)对比操作符 #Isnull(expr)操作符 #如果expr是null,则返回1,否则返回0 mysql> SELECT ISNULL(1+1); #0 mysql> SELECT ISNULL(1/0); #1 #LEAST(value1,value2,...) #返回最小值,如果其中有值为null,则返回null mysql> SELECT LEAST(2,0); #0 mysql> SELECT LEAST(34.0,3.0,5.0,767.0); #3.0 mysql> SELECT LEAST('B','A','C'); #'A'
三、逻辑操作符
逻辑操作符返回1 (TRUE), 0 (FALSE), 或者NULL
#NOT, !逻辑操作符代表非操作 mysql> SELECT NOT 10; #0 mysql> SELECT NOT 0; #1 mysql> SELECT NOT NULL; #NULL mysql> SELECT ! (1+1); #0 mysql> SELECT ! 1+1; #1 #And,&&逻辑操作符 mysql> SELECT 1 AND 1; #1 mysql> SELECT 1 AND 0; #0 mysql> SELECT 1 AND NULL; #NULL mysql> SELECT 0 AND NULL; #0 mysql> SELECT NULL AND 0; #0 #Or, ||逻辑操作符 mysql> SELECT 1 OR 1; #1 mysql> SELECT 1 OR 0; #1 mysql> SELECT 0 OR 0; #0 mysql> SELECT 0 OR NULL; #NULL mysql> SELECT 1 OR NULL; #1 #Xor逻辑操作符 mysql> SELECT 1 XOR 1; #0 mysql> SELECT 1 XOR 0; #1 mysql> SELECT 1 XOR NULL; #NULL mysql> SELECT 1 XOR 1 XOR 1; #1
四、分配操作符
分配操作符是指赋值操作
mysql> SELECT @var1, @var2; #NULL, NULL mysql> SELECT @var1 := 1, @var2; #1, NULL mysql> SELECT @var1, @var2; #1, NULL mysql> SELECT @var1, @var2 := @var1; #1, 1 mysql> SELECT @var1, @var2; #1, 1 mysql> SELECT @var1:=COUNT(*) FROM t1; #如果是4 mysql> SELECT @var1; #4 #":="操作符也可以用在update等语句 mysql> SELECT @var1; #4 mysql> SELECT * FROM t1; #1, 3, 5, 7 mysql> UPDATE t1 SET c1 = 2 WHERE c1 = @var1:= 1; mysql> SELECT @var1; #1 mysql> SELECT * FROM t1; #2, 3, 5, 7 #=操作符在两种情况下会被认为是赋值操作,而其他情况下会认为是对比操作符 #在set语句中,=操作符会被认为是赋值操作 mysql> set @a=1; mysql> select @a; #1 #在update语句中的set子句中,=操作符会被认为是赋值操作
五、流程控制函数
流程控制函数包含以下四种函数
#语法格式 CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END #当value等于compare_value时,则返回result,否则返回else里的result,如果没有else子句则返回null mysql> SELECT CASE 1 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'more' END; #'one' #语法格式 CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END #当第一个condition满足时,则返回result,否则返回else里的result,如果没有else子句时则返回null mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END; #'true' mysql> SELECT CASE BINARY 'B' WHEN 'a' THEN 1 WHEN 'b' THEN 2 END; #NULL --------------------------------------------------------------------------------------------------------- IF(expr1,expr2,expr3) #当expr1为1/true时,则返回expr2,否则返回expr3 mysql> SELECT IF(1>2,2,3); #3 mysql> SELECT IF(1<2,'yes','no'); #'yes' mysql> SELECT IF(STRCMP('test','test1'),'no','yes'); #select STRCMP('test','test1');==>-1 #'no' --------------------------------------------------------------------------------------------------------- IFNULL(expr1,expr2) #当expr1为非null时,则返回expr1,否则返回expr2 mysql> SELECT IFNULL(1,0); #1 mysql> SELECT IFNULL(NULL,10); #10 mysql> SELECT IFNULL(1/0,10); #10 mysql> SELECT IFNULL(1/0,'yes'); #'yes' --------------------------------------------------------------------------------------------------------- NULLIF(expr1,expr2) #当expr1等于expr2时,则返回null,否则返回expr1 mysql> SELECT NULLIF(1,1); #NULL mysql> SELECT NULLIF(1,2); #1
补充:
select gender,if(gender=1,'male','female') from Students; select sname,ifnull(sname,'unknown') from Students;
六、字符串函数
6.1、ASCII(str)
返回str字符串中最左边字符的ascii码值,如果是空串则返回0,如果str是null则返回null
mysql> SELECT ASCII('2'); #50 mysql> SELECT ASCII(2); #50 mysql> SELECT ASCII('dx'); #100 mysql> SELECT ASCII('d'); #100
6.2、CHAR(N,... [USING charset_name])
将括号中的N转化成ascii码对应的字符,返回这些字符组成的字符串,其中的null会被忽略
mysql> SELECT CHAR(77,121,83,81,'76'); #'MySQL' -> 'MySQL' mysql> SELECT CHAR(77,77.3,'77.3'); #'MMM' -> 'MMM‘ mysql> SELECT CHARSET(CHAR(X'65')), CHARSET(CHAR(X'65' USING utf8)); +----------------------+---------------------------------+ | CHARSET(CHAR(X'65')) | CHARSET(CHAR(X'65' USING utf8)) | +----------------------+---------------------------------+ | binary | utf8 | +----------------------+---------------------------------+
6.3、CHAR_LENGTH(str)
返回字符串的字符长度
mysql> select char_length('mysql'); +----------------------+ | char_length('mysql') | +----------------------+ | 5 | +----------------------+ 1 row in set (0.00 sec)
6.4、CONCAT(str1,str2,...) *****
返回括号里所有参数字符串连接在一起,当其中有参数为NULL时则返回NULL
mysql> SELECT CONCAT('My', 'S', 'QL'); #'MySQL' mysql> SELECT CONCAT('My', NULL, 'QL'); #NULL mysql> SELECT CONCAT(14.3); #'14.3'
应用:如何提取名和姓?
mysql> select * from name; +-----+-----------+--------+---------+ | sid | sname | gender | dept_id | +-----+-----------+--------+---------+ | 1 | zhang san | 1 | 1 | | 2 | li si | 1 | 1 | | 3 | wang wu | 1 | 1 | +-----+-----------+--------+---------+ 3 rows in set (0.00 sec) mysql> select sname,substring(sname,1,locate(' ',sname)-1) as firstname,substring(sname,locate(' ',sname)+1,100) as lastname from name; +-----------+-----------+----------+ | sname | firstname | lastname | +-----------+-----------+----------+ | zhang san | zhang | san | | li si | li | si | | wang wu | wang | wu | +-----------+-----------+----------+ 3 rows in set (0.00 sec)
6.5、CONCAT_WS(separator,str1,str2,...)
返回以第一个参数为分隔符的连接后的一个字符串,当有参数为NULL时则null被忽略
mysql> SELECT CONCAT_WS(',','First name','Second name','Last Name'); #'First name,Second name,Last Name' mysql> SELECT CONCAT_WS(',','First name',NULL,'Last Name'); #'First name,Last Name'
6.6、INSERT(str,pos,len,newstr)
将str中从pos位置开始后的len个字符替换成newstr字符串
mysql> SELECT INSERT('Quadratic', 3, 4, 'What'); #'QuWhattic' mysql> SELECT INSERT('Quadratic', -1, 4, 'What'); #'Quadratic' mysql> SELECT INSERT('Quadratic', 3, 100, 'What'); #'QuWhat'
6.7、INSTR(str,substr)
返回str字符串中第一个出现substr字符串的位置
mysql> SELECT INSTR('foobarbar', 'bar'); #4 mysql> SELECT INSTR('xbar', 'foobar'); #0
6.8、LEFT(str,len)
返回str字符串中从左边开始的len个长度的字符
mysql> SELECT LEFT('foobarbar', 5); #'fooba'
6.9、LENGTH(str)
返回str字符串的byte字节长度
mysql> SELECT LENGTH('text'); #4
6.10、LOAD_FILE(file_name)
读取文件且返回文件内容为字符串
mysql> UPDATE t SET blob_col=LOAD_FILE('/tmp/picture') WHERE id=1;
6.11、LOCATE(substr,str)
返回str字符串中第一次出现substr字符串的位置,如果没有则返回null
mysql> SELECT LOCATE('bar', 'foobarbar'); #4 mysql> SELECT LOCATE('xbar', 'foobar'); #0
6.12、LOCATE(substr,str,pos)
返回str字符串中从pos位置开始第一次出现substr字符串的位置,如果没有则返回null
mysql> SELECT LOCATE('bar', 'foobarbar', 5); #7
6.13、LOWER(str)
返回将str字符串中所有字符变换成小写后的字符串,但对二进制文本无效
mysql> SELECT LOWER('QUADRATICALLY'); -> 'quadratically' mysql> SET @str = BINARY 'New York'; mysql> SELECT LOWER(@str), LOWER(CONVERT(@str USING latin1)); +-------------+-----------------------------------+ | LOWER(@str) | LOWER(CONVERT(@str USING latin1)) | +-------------+-----------------------------------+ | New York | new york | +-------------+-----------------------------------+
6.14、LPAD(str,len,padstr)
将str的左边补充为padstr,直到补充成len长度的字符串并返回;如果str的长度比len长,则返回str中最左边开始的len长度的字符
mysql> SELECT LPAD('hi',4,'??'); #'??hi' mysql> SELECT LPAD('hi',1,'??'); #'h'
6.15、LTRIM(str)
将str最左边的空格去掉并返回
mysql> SELECT LTRIM(' barbar'); #'barbar'
6.16、REPEAT(str,count)
将str重复count并组合成字符串返回,如果count<1,则返回空串
mysql> SELECT REPEAT('MySQL', 3); #'MySQLMySQLMySQL'
6.17、REPLACE(str,from_str,to_str)
将所有str字符串中匹配from_str子串的地方都替换成to_str子字符串
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww'); #'WwWwWw.mysql.com'
6.18、REVERSE(str)
将str字符串中的字符按照倒序组合并返回
mysql> SELECT REVERSE('abc'); #'cba'
6.19、RIGHT(str,len)
将str字符串中从右边开始的len个字符返回
mysql> SELECT RIGHT('foobarbar', 4); #'rbar'
6.20、RPAD(str,len,padstr)
将字符串str从右边开始补充为padstr直到整体长度为len,如果str的长度本身大于len,则返回str中len长度的字符串
mysql> SELECT RPAD('hi',5,'?'); #'hi???' mysql> SELECT RPAD('hi',1,'?'); #'h'
6.21、RTRIM(str)
将字符串str右边的空格去掉并返回
mysql> SELECT RTRIM('barbar '); #'barbar'
6.22、SPACE(N)
返回N个长度的空格组成的空字符串
mysql> SELECT SPACE(6);
6.23、SUBSTRING *****
SUBSTRING(str,pos), SUBSTRING(str FROM pos), SUBSTRING(str,pos,len), SUBSTRING(str FROM pos FOR len)
如果没有len参数,则返回从pos位置开始的str中的子字符串;如果有len参数,则从pos位置开始返回str中长度为len的子字符串;如果pos为负值,则代表pos从右边开始数
mysql> SELECT SUBSTRING('Quadratically',5); #'ratically' mysql> SELECT SUBSTRING('foobarbar' FROM 4); #'barbar' mysql> SELECT SUBSTRING('Quadratically',5,6); #'ratica' mysql> SELECT SUBSTRING('Sakila', -3); #'ila' mysql> SELECT SUBSTRING('Sakila', -5, 3); #'aki' mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2); #'ki'
6.24、SUBSTRING_INDEX(str,delim,count)
当count为正数,则返回delim出现在str字符串中第count次之前的子字符串,如果是负数,则从右边开始计算
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2); #'www.mysql' mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2); #'mysql.com'
6.25、UPPER(str)
返回将str字符串中所有字符转换成大写的字符串
mysql> SELECT UPPER('Hej'); #'HEJ'
七、字符串对比函数
#通配符%表示匹配0个或多个字符 #通配符_表示匹配1个字符 mysql> SELECT 'David!' LIKE 'David_'; #1 mysql> SELECT 'David!' LIKE '%D%v%'; #1 #当匹配字符中有特殊字符时,可以用 \或者escape来指定特殊字符为字符 mysql> SELECT 'David!' LIKE 'David\_'; #0 mysql> SELECT 'David_' LIKE 'David\_'; #1 mysql> SELECT 'David_' LIKE 'David|_' ESCAPE '|'; #1 #expr NOT LIKE pat [ESCAPE 'escape_char']是上一个字符串对比函数的反义 STRCMP(expr1,expr2) #当expr1等于expr2时等于0,当expr1小于expr2时为-1,反之为1 mysql> SELECT STRCMP('text', 'text2'); #-1 mysql> SELECT STRCMP('text2', 'text'); #1 mysql> SELECT STRCMP('text', 'text'); #0 mysql> SET @s1 = _latin1 'x' COLLATE latin1_general_ci; #大小写不敏感 mysql> SET @s2 = _latin1 'X' COLLATE latin1_general_ci; mysql> SET @s3 = _latin1 'x' COLLATE latin1_general_cs; #大小写敏感 mysql> SET @s4 = _latin1 'X' COLLATE latin1_general_cs; mysql> SELECT STRCMP(@s1, @s2), STRCMP(@s3, @s4); +------------------+------------------+ | STRCMP(@s1, @s2) | STRCMP(@s3, @s4) | +------------------+------------------+ | 0 | 1 | +------------------+------------------+
八、算数操作符
#/和DIV #/代表除法;div代表整数型除法,相除之后只取整数部分 mysql> SELECT 3/5; #0.60 mysql> SELECT 102/(1-1); #NULL mysql> SELECT 5 DIV 2, -5 DIV 2, 5 DIV -2, -5 DIV -2; #2, -2, -2, 2
九、数字函数
#ABS(X) 绝对值函数 mysql> SELECT ABS(2); #2 mysql> SELECT ABS(-32); #32 #CEILING(X)/CEIL(X) 返回>=X值的最小整数 mysql> SELECT CEILING(1.23); #2 mysql> SELECT CEILING(-1.23); #-1 #FLOOR(X) 返回<=X值的最大整数 mysql> SELECT FLOOR(1.23), FLOOR(-1.23); #1, -2 #MOD(N,M), N % M, N MOD M 取余数操作,返回两者相除之后剩下的余数 mysql> SELECT MOD(234, 10); #4 mysql> SELECT 253 % 7; #1 mysql> SELECT MOD(29,9); #2 mysql> SELECT 29 MOD 9; #2 mysql> SELECT MOD(34.5,3); #1.5 #RAND([N]) 获取0到1之间的随机小数,比如当想获取7~12之间的随机整数是可以使用 SELECT FLOOR(7 + (RAND() * 5)); Select * from students order by rand() limit 1; #ROUND(X), ROUND(X,D) 四舍五入为D位个小数,当D参数不存在是,则默认为0 mysql> SELECT ROUND(-1.23); #-1 mysql> SELECT ROUND(-1.58); #-2 mysql> SELECT ROUND(1.58); #2 mysql> SELECT ROUND(1.298, 1); #1.3 mysql> SELECT ROUND(1.298, 0); #1 mysql> SELECT ROUND(23.298, -1); #20 #TRUNCATE(X,D) 数字X只保留D位的小数,其余均舍弃 mysql> SELECT TRUNCATE(1.223,1); #1.2 mysql> SELECT TRUNCATE(1.999,1); #1.9 mysql> SELECT TRUNCATE(1.999,0); #1 mysql> SELECT TRUNCATE(-1.999,1); #-1.9 mysql> SELECT TRUNCATE(122,-2); #100 mysql> SELECT TRUNCATE(10.28*100,0); #1028
十、日期和时间函数
ADDDATE(date,INTERVAL expr unit), ADDDATE(expr,days) #当expr为正数时则为增加时间,为负数时则为减少时间 #Unit参数可以是任意时间单位 mysql> SELECT ADDDATE('2008-01-02', INTERVAL 31 DAY); #'2008-02-02' mysql> SELECT ADDDATE('2008-01-02', 31); #'2008-02-02' ADDTIME(expr1,expr2) #将expr2的时间增加到expr1上 mysql> SELECT ADDTIME('2007-12-31 23:59:59.999999', '1 1:1:1.000002'); #'2008-01-02 01:01:01.000001' mysql> SELECT ADDTIME('01:00:00.999999', '02:00:00.999998'); #'03:00:01.999997' CONVERT_TZ(dt,from_tz,to_tz) #将时间dt从from_tz这个时区转换成to_tz这个时区并返回 mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET'); #'2004-01-01 13:00:00' mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00'); #'2004-01-01 22:00:00' CURDATE(), CURRENT_DATE, CURRENT_DATE() #返回以yyyy-mm-dd或者yyyymmdd格式的当前时间 mysql> SELECT CURDATE(); #'2008-06-13' mysql> SELECT CURDATE() + 0; #20080613 CURTIME(),CURRENT_TIME,CURRENT_TIME() #按照hh:mm:ss或者hhmmss格式返回当前时间 mysql> SELECT CURTIME(); #'23:50:26' mysql> SELECT CURTIME() + 0; #235026.000000 Now(), CURRENT_TIMESTAMP,CURRENT_TIMESTAMP() #返回当前的日期和时间,格式为yyyy-mm-dd hh:mi:ss或者yyyymmddhhmiss mysql> SELECT NOW(); #'2007-12-15 23:50:26' mysql> SELECT NOW() + 0; #20071215235026.000000 DATE(expr) #获取expr中的日期 mysql> SELECT DATE('2003-12-31 01:02:03'); #'2003-12-31' DATEDIFF(expr1,expr2) #返回expr1和expr2之间的天数差异,忽略时分秒 mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30'); #1 mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31'); #-31 DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr unit) #增加或者减少时间 mysql> SELECT DATE_ADD('2000-12-31 23:59:59',INTERVAL 1 SECOND); #'2001-01-01 00:00:00' mysql> SELECT DATE_ADD('2010-12-31 23:59:59',INTERVAL 1 DAY); #'2011-01-01 23:59:59' mysql> SELECT DATE_ADD('2100-12-31 23:59:59',INTERVAL '1:1' MINUTE_SECOND); #'2101-01-01 00:01:00' mysql> SELECT DATE_SUB('2005-01-01 00:00:00',INTERVAL '1 1:1:1' DAY_SECOND); #'2004-12-30 22:58:59' mysql> SELECT DATE_ADD('1900-01-01 00:00:00',INTERVAL '-1 10' DAY_HOUR); #'1899-12-30 14:00:00' mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY); #'1997-12-02' mysql> SELECT DATE_ADD('1992-12-31 23:59:59.000002',INTERVAL '1.999999' SECOND_MICROSECOND); #'1993-01-01 00:00:01.000001' mysql> SELECT DATE_ADD('2009-01-30', INTERVAL 1 MONTH); #'2009-02-28' DATE_FORMAT(date,format) #将date日期时间转换成format格式 文档:https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-add mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y'); #'Sunday October 2009' mysql> SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s'); #'22:23:00' mysql> SELECT DATE_FORMAT('1900-10-04 22:23:00','%D %y %a %d %m %b %j'); #'4th 00 Thu 04 10 Oct 277' mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00','%H %k %I %r %T %S %w'); #'22 22 10 10:23:00 PM 22:23:00 00 6' mysql> SELECT DATE_FORMAT('1999-01-01', '%X %V'); #'1998 52' mysql> SELECT DATE_FORMAT('2006-06-00', '%d'); #'00' DAY(date), DAYOFMONTH(date) #返回date中日期在当前月份中是第几天 mysql> SELECT DAYOFMONTH('2007-02-03'); #3 DAYNAME(date) #返回date时间是星期几 mysql> SELECT DAYNAME('2007-02-03'); #'Saturday’ DAYOFWEEK(date) #返回date时间是星期“几” mysql> SELECT DAYOFWEEK('2007-02-03'); #7 DAYOFYEAR(date) #返回date是一年中的第几天,取值范围在1~366 mysql> SELECT DAYOFYEAR('2007-02-03'); #34 EXTRACT(unit FROM date) #Unit单元和date_add/date_sub函数中的一样,是获取date日期的unit部分 mysql> SELECT EXTRACT(YEAR FROM '2009-07-02'); #2009 mysql> SELECT EXTRACT(YEAR_MONTH FROM '2009-07-02 01:02:03'); #200907 mysql> SELECT EXTRACT(DAY_MINUTE FROM '2009-07-02 01:02:03'); #20102 mysql> SELECT EXTRACT(MICROSECOND FROM '2003-01-02 10:30:00.000123'); #123 FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format) #根据给定的unixtime,返回yyyy-mm-dd hh:mi:ss或者yyyymmddhhmiss格式的具体时间,或者按照format返回时间 mysql> SELECT FROM_UNIXTIME(1447430881); #'2015-11-13 10:08:01' mysql> SELECT FROM_UNIXTIME(1447430881) + 0; #20151113100801 mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x'); #'2015 13th November 10:08:01 2015' LAST_DAY(date) #返回date日期所在月份的最后一天日期 mysql> SELECT LAST_DAY('2004-02-05'); #'2004-02-29' mysql> SELECT LAST_DAY('2004-01-01 01:01:01'); #'2004-01-31' mysql> SELECT LAST_DAY('2003-03-32'); #NULL SYSDATE() #返回当前日期和时间,格式为yyyy-mm-dd hh:mi:ss或者yyyymmddhhmiss #和now()函数的区别在于now()返回的时间是语句执行的时间,而sysdate()返回的时间是该函数执行的时间 mysql> SELECT NOW(), SLEEP(2), NOW(); +---------------------+----------+---------------------+ | NOW() | SLEEP(2) | NOW() | +---------------------+----------+---------------------+ | 2019-10-06 22:41:52 | 0 | 2019-10-06 22:41:52 | +---------------------+----------+---------------------+ 1 row in set (2.00 sec) mysql> SELECT SYSDATE(), SLEEP(2), SYSDATE(); +---------------------+----------+---------------------+ | SYSDATE() | SLEEP(2) | SYSDATE() | +---------------------+----------+---------------------+ | 2019-10-06 22:42:16 | 0 | 2019-10-06 22:42:18 | +---------------------+----------+---------------------+ 1 row in set (2.00 sec) TIME(expr) #返回expr日期时间中的时间部分 mysql> SELECT TIME('2003-12-31 01:02:03'); #'01:02:03' mysql> SELECT TIME('2003-12-31 01:02:03.000123'); #'01:02:03.000123' UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date) #如果没有date参数,则返回当前时间到1970-01-01 00:00:00之间的秒数,如果有date参数,则表示date到1970-01-01 00:00:00之间的秒数 mysql> SELECT UNIX_TIMESTAMP(); #1447431666 mysql> SELECT UNIX_TIMESTAMP('2015-11-13 10:20:19'); #1447431619
十一、格式转换函数
#Cast()和convert()两个函数都可以用来转换数据类型或者转换字符集 #允许转换的数据类型包括: Binary[N] char[N] Date Datetime decimal[M,[D]] Time Signed [integer] Unsigned [integer] SELECT CONVERT(_latin1'Müller' USING utf8); SELECT CONVERT('test', CHAR CHARACTER SET utf8); SELECT CAST('test' AS CHAR CHARACTER SET utf8); select CAST('2000-01-01' AS DATE); select CONVERT('2000-01-01', DATE);
十二、聚合函数
#用在存在group by子句的语句中 AVG([DISTINCT] expr) #计算expr的平均值,distinct关键词表示是否排除重复值 mysql> SELECT student_name, AVG(test_score) FROM student GROUP BY student_name; COUNT(expr) #计算expr中的个数,如果没有匹配则返回0,注意NULL的区别 mysql> SELECT student.student_name,COUNT(*) FROM student,course WHERE student.student_id=course.student_id GROUP BY student_name; COUNT(DISTINCT expr,[expr...]) #计算有多少个不重复的expr值,注意是计算非NULL的个数 mysql> SELECT COUNT(DISTINCT results) FROM student; MAX([DISTINCT] expr),MIN([DISTINCT] expr) #返回expr中最大或者最小的值 mysql> SELECT student_name, MIN(test_score), MAX(test_score) FROM student GROUP BY student_name; SUM([DISTINCT] expr) #返回expr的求和值
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!