单行函数: 输入一行,输出一行
聚合函数(分组函数): 输入多行,输出一行
单行函数: 输入一行,输出一行 聚合函数(分组函数): 输入多行,输出一行 #1.数值函数 #基本的操作 SELECT ABS(-123),ABS(32),SIGN(-23),SIGN(43),PI(),CEIL(32.32),CEILING(-43.23),FLOOR(32.32), FLOOR(-43.23),MOD(12,5),12 MOD 5,12 % 5 FROM DUAL; ABS: 取绝对值 SIGN: 整数返回1,负数返回负1,0返回0 PI: π圆周率 CEIL: 天花板函数,因32.32在32-33之间,所以天花板数是33,负数43.23在-44- -43之间,所以天花板是-43 FLOOR: 地板函数,跟上面一个原理 MOD: 取余数 #取随机数 #不放数值返回随机数且每次返回不同,放相同数值不论正负,返回相同数值 SELECT RAND(),RAND(),RAND(10),RAND(10),RAND(-1),RAND(-1) FROM DUAL; #四舍五入,截断操作 #y为保留小数位 #当为负数,则以此类推-1为2,当3小于5不进位为0,返回结果为120 #-25等于5进一位,返回200 SELECT ROUND(123.556),ROUND(123.456,0),ROUND(123.456,1),ROUND(123.456,2), ROUND(123.456,-1),ROUND(153.456,-2) FROM DUAL; # 截断操作 # 仅截断,不做进位 # -1 返回120 SELECT TRUNCATE(123.456,0),TRUNCATE(123.496,1),TRUNCATE(129.45,-1) FROM DUAL; #单行函数可以嵌套 SELECT TRUNCATE(ROUND(123.456,2),0) FROM DUAL; #角度与弧度的互换 SELECT RADIANS(30),RADIANS(45),RADIANS(60),RADIANS(90), #角度换弧度 DEGREES(2*PI()),DEGREES(RADIANS(60)) # 弧度换角度 FROM DUAL; #三角函数 SELECT SIN(RADIANS(30)),DEGREES(ASIN(1)),TAN(RADIANS(45)),DEGREES(ATAN(1)) FROM DUAL; #指数和对数 SELECT POW(2,5),POWER(2,4),EXP(2) FROM DUAL; SELECT LN(EXP(2)),LOG(EXP(2)),LOG10(10),LOG2(4) FROM DUAL; #进制间的转换 # BIN转2进制 # HEX转16进制 # OCT转8进制 # SELECT BIN(10),HEX(10),OCT(10),CONV(10,10,8) # 第二个10表示进制,将10进制的10转为8进制 FROM DUAL;
字符串函数
#只返回第一个字符的编码
#返回5,2,字符个数
#字节数,一个中文占3个字节
SELECT ASCII('Abcdfsf'),CHAR_LENGTH('hello'),CHAR_LENGTH('我们'), LENGTH('hello'),LENGTH('我们') FROM DUAL;
#
# # xxx worked for yyy SELECT CONCAT(emp.last_name,' worked for ',mgr.last_name) "details" FROM employees emp JOIN employees mgr WHERE emp.`manager_id` = mgr.employee_id; SELECT CONCAT_WS('-','hello','world','hello','beijing') FROM DUAL; #字符串的索引是从1开始的! SELECT INSERT('helloworld',2,3,'aaaaa'),REPLACE('hello','lol','mmm') FROM DUAL; SELECT UPPER('HelLo'),LOWER('HelLo') FROM DUAL; SELECT last_name,salary FROM employees WHERE LOWER(last_name) = 'King'; SELECT LEFT('hello',2),RIGHT('hello',3),RIGHT('hello',13) FROM DUAL; # LPAD:实现右对齐效果 # RPAD:实现左对齐效果 SELECT employee_id,last_name,LPAD(salary,10,' ') FROM employees; SELECT CONCAT('---',LTRIM(' h el lo '),'***'), TRIM('oo' FROM 'ooheollo') FROM DUAL; SELECT REPEAT('hello',4),LENGTH(SPACE(5)),STRCMP('abc','abe') FROM DUAL; SELECT SUBSTR('hello',2,2),LOCATE('lll','hello') FROM DUAL; SELECT ELT(2,'a','b','c','d'),FIELD('mm','gg','jj','mm','dd','mm'), FIND_IN_SET('mm','gg,mm,jj,dd,mm,gg') FROM DUAL; SELECT employee_id,NULLIF(LENGTH(first_name),LENGTH(last_name)) "compare" FROM employees;
标签:10,函数,RADIANS,单行,DUAL,MySQL,hello,SELECT From: https://www.cnblogs.com/rtnb/p/16883452.html