Mysql study concluded (5) - MySql commonly used functions Daquan explain

MySQL database provides a very rich function. MySQL function comprising mathematical functions, string functions, date and time functions, conditional functions, system information function, an encryption function, and the like formatting functions. With these functions, the user's operation can be simplified. For example, string concatenation function can easily be connected together multiple strings. In this talk will explain include:
a mathematical function
string functions
date and time functions
condition judging function
system function information
encryption function
formatting function

       MySQL MySQL database function is provided by an internal function. These internal functions can help users to more easily process the data table. This section will briefly what kinds of functions, as well as the role and use of these types of functions contained in MySQL. MySQL function comprising mathematical functions, string functions, date and time functions, conditional functions, system information function, an encryption function and the like. SELECT statement and its conditional expression can use these functions. Meanwhile, INSERT, UPDATE, DELECT statements and conditional expressions can also use these functions. For example, a data table is negative, this data will now need to be positive. This allows use of the absolute value function. Be understood from the above, MySQL corresponding processing functions may be performed on the data in the table, the user wishes to obtain the data obtained. These functions can make the function more powerful MySQL database.

A mathematical function

MySQL mathematical functions are commonly used in a class of functions. Mainly for digital processing, including integer, floating point and so on. Mathematical functions include the absolute value function, a sine function, cosine function, a function of acquiring a random number, and the like.

ABS (X): X returns the absolute value
select ABS (-32); 

MOD(N,M)或%:返回N被M除的余数。 
select MOD(15,7); 
select 15 % 7; 

FLOOR(X):返回不大于X的最大整数值。 
select FLOOR(1.23); 
select FLOOR(-1.23); 

  
CEILING(X):返回不小于X的最小整数值。 
select CEILING(1.23); 
select CEILING(-1.23); 

ROUND(X) :返回参数X的四舍五入的一个整数。 
select ROUND(1.58); 
select ROUND(-1.58); 


二、字符串函数

ASCII(str):返回字符串str的最左面字符的ASCII代码值。如果str是空字符串,返回0。如果str是NULL,返回NULL。 
select ASCII('2'); 
select ASCII(2); 
select ASCII('dx') 


CONCAT(str1,str2,...):返回来自于参数连结的字符串。如果任何参数是NULL,返回NULL。可以有超过2个的参数。一个数字参数被变换为等价的字符串形式。 
select CONCAT('My', 'S', 'QL'); 
select CONCAT('My', NULL, 'QL'); 
select CONCAT(14.3); 

LENGTH(str):返回字符串str的长度。
select LENGTH('text'); 


LOCATE(substr,str):返回子串substr在字符串str第一个出现的位置,如果substr不是在str里面,返回0. 
select LOCATE('bar', 'foobarbar'); 
select LOCATE('xbar', 'foobar'); 

INSTR(str,substr):返回子串substr在字符串str中的第一个出现的位置。
select INSTR('foobarbar', 'bar'); 
select INSTR('xbar', 'foobar'); 


LEFT(str,len):返回字符串str的最左面len个字符。
select LEFT('foobarbar', 5); 

RIGHT(str,len):返回字符串str的最右面len个字符。 
select RIGHT('foobarbar', 4); 


SUBSTRING(str,pos):从字符串str的起始位置pos返回一个子串。 
select SUBSTRING('Quadratically',5);

TRIM(str):返回字符串str,所有前缀或后缀被删除了。
select TRIM(' bar ');   


LTRIM(str):返回删除了其前置空格字符的字符串str。
select LTRIM(' barbar'); 

RTRIM(str):返回删除了其拖后空格字符的字符串str。
select RTRIM(‘barbar ’); 

  
REPLACE(str,from_str,to_str):返回字符串str,其字符串from_str的所有出现由字符串to_str代替。 

select REPLACE('www.mysql.com', 'w', 'Ww'); 

REPEAT(str,count):返回由重复countTimes次的字符串str组成的一个字符串。如果count <= 0,返回一个空字符串。如果str或count是NULL,返回NULL。 
select REPEAT('MySQL', 3); 


REVERSE(str):返回颠倒字符顺序的字符串str。 
select REVERSE('abc');

INSERT(str,pos,len,newstr):返回字符串str,在位置pos起始的子串且len个字符长的子串由字符串newstr代替。 
select INSERT(‘whatareyou', 5, 3, ‘is'); 

  

三、日期和时间函数 

DAYOFWEEK(date):返回日期date的星期索引(1=星期天,2=星期一, …7=星期六)。
select DAYOFWEEK('1998-02-03'); 
select DAYOFWEEK(now()); 

WEEKDAY(date):返回date的星期索引(0=星期一,1=星期二, ……6= 星期天)。
select WEEKDAY('1997-11-05'); 

DAYOFMONTH(date):返回date的月份中的日期,在1到31范围内。 
select DAYOFMONTH('1998-02-03'); 

DAYOFYEAR(date):返回date在一年中的日数, 在1到366范围内。 
select DAYOFYEAR('1998-02-03'); 

MONTH(date):返回date的月份,范围1到12。 
select MONTH('1998-02-03'); 

DAYNAME(date):返回date的星期名字。 
select DAYNAME("1998-02-05"); 

MONTHNAME(date) :返回date的月份名字。 
select MONTHNAME("1998-02-05"); 


QUARTER(date):返回date一年中的季度,范围1到4。 
select QUARTER('98-04-01'); 


WEEK(date,first):对于星期天是一周的第一天的地方,有一个单个参数,返回date的周数,范围在0到52。2个参数形式WEEK()允许你指定星期是否开始于星期天或星期一。如果第二个参数是0,星期从星期天开始,如果第二个参数是1,从星期一开始。 
select WEEK('2009-02-20'); 
select WEEK('2009-02-20',0); 
select WEEK('2009-02-20',1); 


YEAR(date):返回date的年份,范围在1000到9999。 
select YEAR('98-02-03'); 

HOUR(time):返回time的小时,范围是0到23。
select HOUR('10:05:03'); 

MINUTE(time):返回time的分钟,范围是0到59。 
select MINUTE('98-02-03 10:05:03'); 

SECOND(time):回来time的秒数,范围是0到59。 
select SECOND('10:05:03'); 

  
DATE_ADD(date,INTERVAL expr type) ,进行日期增加的操作,可以精确到秒
DATE_SUB(date,INTERVAL expr type) ,进行日期减少的操作,可以精确到秒

SELECT "1997-12-31 23:59:59" + INTERVAL 1 SECOND; 

SELECT INTERVAL 1 DAY + "1997-12-31"; 
SELECT "1998-01-01" - INTERVAL 1 SECOND; 
SELECT DATE_ADD("1997-12-31 23:59:59", INTERVAL 1 SECOND); 
SELECT DATE_ADD("1997-12-31 23:59:59", INTERVAL "1:1" MINUTE_SECOND); 

SELECT DATE_SUB("1998-01-01 00:00:00", INTERVAL "1 1:1:1" DAY_SECOND); 
SELECT DATE_SUB("1998-01-02", INTERVAL 31 DAY); 

CURRENT_DATE:以‘YYYY-MM-DD’或YYYYMMDD格式返回今天日期值,取决于函数在一个字符串还是数字上下文被使用。
select CURDATE(); 

CURRENT_TIME:以‘HH:MM:SS’或HHMMSS格式返回当前时间值
select CURTIME(); 

NOW():以‘YYYY-MM-DD HH:MM:SS’或YYYYMMDDHHMMSS格式返回当前的日期和时间 
select NOW(); 


四、控制流程函数  

CASE value WHEN [compare-value] THEN result [WHEN [compare-value] THEN result ...] [ELSE result] END CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END 
在第一个方案的返回结果中, value=compare-value。而第二个方案的返回结果是第一种情况的真实结果。如果没有匹配的结果值,则返回结果为ELSE后的结果,如果没有ELSE 部分,则返回值为 NULL。
SELECT CASE 11 WHEN 1 THEN 'one'
WHEN 2 THEN 'two' ELSE 'more' END;
SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;

SELECT CASE BINARY 'B'
WHEN 'a' THEN 1 WHEN 'b' THEN 2 END;


IF(expr1,expr2,expr3) 
如果 expr1 是TRUE (expr1 <> 0 and expr1 <> NULL),则 IF()的返回值为expr2; 否则返回值则为 expr3。IF() 的返回值为数字值或字符串值,具体情况视其所在语境而定。
SELECT IF(1>2,2,3);
SELECT IF(1<2,'yes ','no');
SELECT IF(STRCMP('test','test1'),'no','yes');

Strcmp(str1,str2):如果str1>str2返回1,str1=str2反回0,str1<str2返回-1)

STRCMP(expr1,expr2) 

如果字符串相同,STRCMP()返回0,如果第一参数根据当前的排序次序小于第二个,返回-1,否则返回1。
select STRCMP('text', 'text2'); 
select STRCMP('text2', 'text'); 
select STRCMP('text', 'text');


  五、系统信息函数

系统信息函数用来查询MySQL数据库的系统信息。例如,查询数据库的版本,查询数据库的当前用户等。本小节将详细讲解系统信息函数的作用和使用方法。

获取MySQL版本号、连接数、数据库名的函数

VERSION()函数返回数据库的版本号;


CONNECTION_ID()函数返回服务器的连接数,也就是到现在为止MySQL服务的连接次数;


DATABASE()和SCHEMA()返回当前数据库名。


获取用户名的函数

USER()、SYSTEM_USER()、SESSION_USER()、CURRENT_USER()和CURRENT_USER这几个函数可以返回当前用户的名称。


获取字符串的字符集和排序方式的函数

CHARSET(str)函数返回字符串str的字符集,一般情况这个字符集就是系统的默认字符集;COLLATION(str)函数返回字符串str的字符排列方式。



获取最后一个自动生成的ID值的函数
LAST_INSERT_ID()函数返回最后生成的AUTO_INCREMENT值。


六、加密函数

加密函数是MySQL中用来对数据进行加密的函数。因为数据库中有些很敏感的信息不希望被其他人看到,就应该通过加密方式来使这些数据变成看似乱码的数据。例如用户的密码,就应该经过加密。本小节将详细讲解加密函数的作用和使用方法。
下面是各种加密函数的名称、作用和使用方法。

加密函数PASSWORD(str)
PASSWORD(str)函数可以对字符串str进行加密。一般情况下,PASSWORD(str)函数主要是用来给用户的密码加密的。下面使用PASSWORD(str)函数为字符串“abcd”加密。

加密函数MD5(str)

MD5(str)函数可以对字符串str进行加密。MD5(str)函数主要对普通的数据进行加密。下面使用MD5(str)函数为字符串“abcd”加密。


加密函数ENCODE(str,pswd_str)
ENCODE(str,pswd_str)函数可以使用字符串pswd_str来加密字符串str。加密的结果是一个二进制数,必须使用BLOB类型的字段来保存它。
解密函数
DECODE(crypt_str,pswd_str)函数可以使用字符串pswd_str来为crypt_str解密。crypt_str是通过ENCODE(str,pswd_str)加密后的二进制数据。字符串pswd_str应该与加密时的字符串pswd_str是相同的。下面使用DECODE(crypt_str,pswd_str)为ENCODE(str,pswd_str)加密的数据解密。

七、其它函数

格式化函数FORMAT(x,n)

FORMAT(x,n)函数可以将数字x进行格式化,将x保留到小数点后n位。这个过程需要进行四舍五入。例如FORMAT(2.356,2)返回的结果将会是2.36;FORMAT(2.353,2)返回的结果将会是2.35。下面使用FORMAT(x,n)函数来讲235.3456和235.3454进行格式化,都保留到小数点后3位。

不同进制的数字进行转换的函数

ASCII(s)返回字符串s的第一个字符的ASCII码;BIN(x)返回x的二进制编码;HEX(x)返回x的十六进制编码;OCT(x)返回x的八进制编码;CONV(x,f1,f2)将x从f1进制数变成f2进制数。

和下面的

IP地址与数字相互转换的函数

INET_ATON(IP)函数可以将IP地址转换为数字表示;INET_NTOA(n)函数可以将数字n转换成IP的形式。其中,INET_ATON(IP)函数中IP值需要加上引号。这两个函数互为反函数。


加锁函数和解锁函数

GET_LOCT (name, time) function is defined as a name nam, duration time length of the second lock. When successful, returns 1; if you try to overtime, return 0; if an error is encountered, returns NULL. RELEASE_LOCK (name) function to lift the name name of the lock. If the unlocking is successful, returns 1; if you try to overtime, 0 is returned; if unsuccessful, return NULL; IS_FREE_LOCK (name) function to determine whether the lock named name. If used, it returns 0; otherwise, it returns 1.
Function designation operation is repeatedly performed

BENCHMARK (count, expr) function will repeat the expression expr count times, and then return execution time. This function can be used to determine the speed of the process MySQL expression.


Function to change the character set

CONVERT (s USING cs) function into character set string s cs

CAST (x AS type) and CONVERT (x, type) Both functions into type x type. These two functions only work for BINARY, CHAR, DATE, DATETIME, TIME, SIGNED INTEGER, UNSIGNED INTEGER these types. However, both methods only to change the data type of the output value and does not change the type field in the table.

Reproduced in: https: //my.oschina.net/zhanghaiyang/blog/597444

Guess you like

Origin blog.csdn.net/weixin_33889665/article/details/92662558