Who can be a programmer without a database? Today I would like to share the commonly used functions of MySQL that I sorted out. They basically include the functions that I usually use. I can help you!
If you can use the database functions well, other things will follow.
Article directory
1. Process control function
serial number | function | illustrate |
---|---|---|
1 | IF(test,t,f) | If test is true, return t; otherwise, return f |
2 | IFNULL(arg1,arg2) | If arg1 is not null, return arg1, otherwise return arg2 |
3 | NULLIF(arg1,arg2) | If arg1=arg2 return NULL; otherwise return arg1 |
4 | CASE WHEN[test1] THEN [result1]…ELSE [default] END | If testN is true, return resultN, otherwise return default |
5 | CASE [test] WHEN[val1] THEN [result]…ELSE [default]END | If test and valN are equal, return resultN, otherwise return default |
Example: IF(test,v1,v2) returns the result v1 if the expression expr is true; otherwise, returns the result v2
SELECT IF(1>0,'yes','no');
result:
yes
2. Aggregation function
serial number | function | illustrate |
---|---|---|
1 | COUNT(col) | Count the number of rows in the query result |
2 | MIN(col) | Query the minimum value of the specified column |
3 | SUM(col) | Sum, returns the sum of the specified columns |
4 | AVG(col) | Average, returns the average value of the specified column data |
Example: SUM(expression) returns the sum of the specified field
SELECT SUM(age) AS totalAage FROM Student;
result:
age的总和
I have hidden a lot of technical dry goods, fans can go whoring for nothing (click here)
3. Encryption function
serial number | function | illustrate |
---|---|---|
1 | SHA() | Computes the Secure Hash Algorithm (SHA) checksum of the string str |
2 | PASSWORD(str) | Returns the encrypted version of the string str. This encryption process is irreversible and uses a different algorithm from the UNIX password encryption process. |
3 | MD5() | Computes the MD5 checksum of the string str |
4 | ENCRYPT(str,salt) | Use the UNIXcrypt() function to encrypt the string str with the keyword salt (a string that can uniquely determine the password, just like a key) |
5 | ENCODE(str,key) | Use key as the key to encrypt the string str, and the result of calling ENCODE() is a binary string, which is stored in BLOB type |
Example 1: Implement the ENCODE function on a string.
SELECT
ENCODE('geeksforgeeks', 'passwordstring');
result:
Q)?P????j[K
4. Date function
serial number | function | illustrate |
---|---|---|
1 | CURDATE() | return current date |
2 | CURRENT_DATE() | return current date |
3 | CURRENT_TIME | return current time |
4 | CURRENT_TIMESTAMP() | returns the current date and time |
5 | CURTIME() | return current time |
6 | DATE() | Extract date values from date or datetime expressions |
7 | DATEDIFF(d1,d2) | Calculate the number of days between dates d1->d2 |
8 | YEAR(d) | return year |
9 | YEARWEEK(date, mode) | Return the year and week (0 to 53), 0 in mode means Sunday, 1 means Monday, and so on |
10 | WEEKOFYEAR(d) | The calculation date d is the first week of the year, the range is 0 to 53 |
11 | WEEKDAY(d) | Date d is the day of the week, 0 means Monday, 1 means Tuesday |
12 | WEEK(d) | The calculation date d is the first week of the year, the range is 0 to 53 |
13 | TO_DAYS(d) | Calculate the number of days from January 1, 0000 to date d |
14 | TIMESTAMP(expression, interval) | With a single argument, the function returns a date or datetime expression; with 2 arguments, the arguments are summed |
15 | TIMEDIFF(time1, time2) | Compute time difference |
16 | TIME_TO_SEC(t) | Convert time t to seconds |
17 | TIME_FORMAT(t,f) | display time t as required by expression f |
18 | TIME(expression) | Extract the time portion of the passed in expression |
19 | SYSDATE() | returns the current date and time |
20 | SUBTIME(t,n) | time t minus n seconds |
21 | SUBDATE(d,n) | the date after subtracting n days from date d |
22 | STR_TO_DATE(string, format_mask) | convert string to date |
23 | SEC_TO_TIME(s) | Convert time s in seconds to hours minutes seconds format |
24 | SECOND(t) | 返回 t 中的秒钟值 |
25 | QUARTER(d) | 返回日期d是第几季节,返回 1 到 4 |
26 | SECOND(t) | 返回 t 中的秒钟值 |
27 | QUARTER(d) | 返回日期d是第几季节,返回 1 到 4 |
28 | PERIOD_DIFF(period1, period2) | 返回两个时段之间的月份差值 |
29 | PERIOD_ADD(period, number) | 为 年-月 组合日期添加一个时段 |
30 | NOW() | 返回当前日期和时间 |
31 | MONTH(d) | 返回日期d中的月份值,1 到 12 |
32 | MONTHNAME(d) | 返回日期当中的月份名称,如 November |
33 | MINUTE(t) | 返回 t 中的分钟值 |
34 | MICROSECOND(date) | 返回日期参数所对应的微秒数 |
35 | MAKETIME(hour, minute, second) | 组合时间,参数分别为小时、分钟、秒 |
36 | MAKEDATE(year, day-of-year) | 基于给定参数年份 year 和所在年中的天数序号 day-of-year 返回一个日期 |
37 | LOCALTIMESTAMP() | 返回当前日期和时间 |
38 | LOCALTIME() | 返回当前日期和时间 |
39 | LAST_DAY(d) | 返回给给定日期的那一月份的最后一天 |
40 | HOUR(t) | 返回 t 中的小时值 |
41 | FROM_DAYS(n) | 计算从 0000 年 1 月 1 日开始 n 天后的日期 |
42 | EXTRACT(type FROM d) | 从日期 d 中获取指定的值,type 指定返回的值。 type可取值为时、分、秒、周等等 |
43 | DAYOFYEAR(d) | 计算日期 d 是本年的第几天 |
44 | DAYOFWEEK(d) | 日期 d 今天是星期几,1 星期日,2 星期一,以此类推 |
45 | DAYOFMONTH(d) | 计算日期 d 是本月的第几天 |
46 | DAYNAME(d) | 返回日期 d 是星期几,如 Saturday,Sunday |
47 | DAY(d) | 返回日期值 d 的日期部分 |
48 | DATE_SUB(date,INTERVAL expr type) | 函数从日期减去指定的时间间隔。 |
49 | DATE_FORMAT(d,f) | 按表达式 f的要求显示日期 d |
50 | ADDTIME(t,n) | n 是一个时间表达式,时间 t 加上时间表达式 n |
51 | ADDDATE(d,n) | 计算起始日期 d 加上 n 天的日期 |
举例:DAYNAME(d)返回日期 d 是星期几,如 Monday,Tuesday
SELECT DAYNAME('2018-6-1 12:45:21');
结果:
Friday
五、数字函数
序号 | 函数 | 说明 |
---|---|---|
1 | SUM(expression) | 返回指定字段的总和 |
2 | SQRT(x) | 返回x的平方根 |
3 | SIN(x) | 求正弦值(参数是弧度) |
4 | SIGN(x) | 返回 x 的符号,x 是负数、0、正数分别返回 -1、0 和 1 |
5 | ROUND(x) | 返回离 x 最近的整数 |
6 | RAND() | 返回 0 到 1 的随机数 |
7 | RADIANS(x) | 将角度转换为弧度 |
8 | POWER(x,y) | 返回 x 的 y 次方 |
9 | POW(x,y) | 返回 x 的 y 次方 |
10 | PI() | 返回圆周率(3.141593) |
11 | MOD(x,y) | 返回 x 除以 y 以后的余数 |
12 | MIN(expression) | 返回字段 expression 中的最小值 |
13 | MAX(expression) | 返回字段 expression 中的最大值 |
14 | LOG2(x) | 返回以 2 为底的对数 |
15 | LOG(x) 或 LOG(base, x) | 返回自然对数(以 e 为底的对数),如果带有 base 参数,则 base 为指定带底数。 |
16 | LN | 返回数字的自然对数,以 e 为底。 |
17 | LEAST(expr1, expr2, expr3, …) | 返回列表中的最小值 |
18 | GREATEST(expr1, expr2, expr3, …) | 返回列表中的最大值 |
19 | FLOOR(x) | 返回小于或等于 x 的最大整数 |
20 | EXP(x) | 返回 e 的 x 次方 |
21 | n DIV m | 整除,n 为被除数,m 为除数 |
22 | DEGREES(x) | 将弧度转换为角度 |
23 | COUNT(expression) | 返回查询的记录总数,expression 参数是一个字段或者 * 号 |
24 | COT(x) | 求余切值(参数是弧度) |
25 | COS(x) | 求余弦值(参数是弧度) |
26 | CEILING(x) | 返回大于或等于 x 的最小整数 |
27 | CEIL(x) | 返回大于或等于 x 的最小整数 |
28 | AVG(expression) | 返回一个表达式的平均值,expression 是一个字段 |
29 | ATAN(x) | 求反正切值(参数是弧度) |
30 | ASIN(x) | 求反正弦值(参数是弧度) |
31 | ACOS(x) | 求 x 的反余弦值(参数是弧度) |
32 | ABS(x) | 返回 x 的绝对值 |
33 | TAN(x) | 求正切值(参数是弧度) |
34 | TRUNCATE(x,y) | 返回数值 x 保留到小数点后 y 位的值(与 ROUND 最大的区别是不会进行四舍五入) |
举例:CEIL(x)/CEILING(x) 返回大于或等于 x 的最小整数
SELECT CEIL(2.5);
SELECT CEILING(2.5);
结果:
3
六、字符串函数
序号 | 函数 | 说明 |
---|---|---|
1 | UPPER(s) | 将字符串转换为大写 |
2 | UCASE(s) | 将字符串转换为大写 |
3 | TRIM(s) | 去掉字符串 s 开始和结尾处的空格 |
4 | SUBSTRING_INDEX(s, delimiter, number) | 返回从字符串 s 的第 number 个出现的分隔符 delimiter 之后的子串。 如果 number 是正数,返回第 number 个字符左边的字符串。 如果 number 是负数,返回第(number 的绝对值(从右边数))个字符右边的字符串。 |
5 | SUBSTRING(s, start, length) | 从字符串 s 的 start 位置截取长度为 length 的子字符串 |
6 | SUBSTR(s, start, length) | 从字符串 s 的 start 位置截取长度为 length 的子字符串 |
7 | STRCMP(s1,s2) | 比较字符串 s1 和 s2,如果 s1 与 s2 相等返回 0 ,如果 s1>s2 返回 1,如果 s1<s2 返回 -1 |
8 | SPACE(n) | 返回 n 个空格 |
9 | RTRIM(s) | 去掉字符串 s 结尾处的空格 |
10 | RPAD(s1,len,s2) | 在字符串 s1 的结尾处添加字符串 s2,使字符串的长度达到 len |
11 | RIGHT(s,n) | 返回字符串 s 的后 n 个字符 |
12 | REVERSE(s) | 将字符串s的顺序反过来 |
13 | REPLACE(s,s1,s2) | 将字符串 s2 替代字符串 s 中的字符串 s1 |
14 | REPEAT(s,n) | 将字符串 s 重复 n 次 |
15 | POSITION(s1 IN s) | 从字符串 s 中获取 s1 的开始位置 |
16 | MID(s,n,len) | 从字符串 s 的 n 位置截取长度为 len 的子字符串,同 SUBSTRING(s,n,len) |
17 | LTRIM(s) | 去掉字符串 s 开始处的空格 |
18 | LPAD(s1,len,s2) | 在字符串 s1 的开始处填充字符串 s2,使字符串长度达到 len |
19 | LOWER(s) | 将字符串 s 的所有字母变成小写字母 |
20 | LEFT(s,n) | 返回字符串 s 的前 n 个字符 |
21 | LCASE(s) | 将字符串 s 的所有字母变成小写字母 |
22 | LOCATE(s1,s) | 从字符串 s 中获取 s1 的开始位置 |
23 | INSERT(s1,x,len,s2) | 字符串 s2 替换 s1 的 x 位置开始长度为 len 的字符串 |
24 | FORMAT(x,n) | 函数可以将数字 x 进行格式化 “#,###.##”, 将 x 保留到小数点后 n 位,最后一位四舍五入。 |
25 | FIND_IN_SET(s1,s2) | 返回在字符串s2中与s1匹配的字符串的位置 |
26 | FIELD(s,s1,s2…) | 返回第一个字符串 s 在字符串列表(s1,s2…)中的位置 |
27 | CONCAT_WS(x, s1,s2…sn) | 同 CONCAT(s1,s2,…) 函数,但是每个字符串之间要加上 x,x 可以是分隔符 |
28 | CONCAT(s1,s2…sn) | 字符串 s1,s2 等多个字符串合并为一个字符串 |
29 | CHARACTER_LENGTH(s) | 返回字符串 s 的字符数 |
30 | CHAR_LENGTH(s) | 返回字符串 s 的字符数 |
31 | ASCII(s) | 返回字符串 s 的第一个字符的 ASCII 码。 |
举例:INSERT(s1,x,len,s2)字符串 s2 替换 s1 的 x 位置开始长度为 len 的字符串
SELECT INSERT("google.com", 1, 5, "baidu");
结果:
baidu
七、其他函数
序号 | 函数 | 说明 |
---|---|---|
1 | USER() | 返回当前用户 |
2 | SESSION_USER() | 返回当前用户 |
3 | SYSTEM_USER() | 返回当前用户 |
4 | CURRENT_USER() | 返回当前用户 |
5 | VERSION() | 返回数据库的版本号 |
6 | DATABASE() | 返回当前数据库名 |
7 | COALESCE(expr1, expr2, …, expr_n) | 返回参数中的第一个非空表达式(从左向右) |
8 | CONNECTION_ID() | 返回唯一的连接 ID |
9 | CONV(x,f1,f2) | 返回 f1 进制数变成 f2 进制数 |
10 | CONVERT(s USING cs) | 函数将字符串 s 的字符集变成 cs |
11 | CAST(x AS type) | 转换数据类型 |
12 | BINARY(s) | 将字符串 s 转换为二进制字符串 |
13 | BIN(x) | 返回 x 的二进制编码 |
14 | IF(expr,v1,v2) | 如果表达式 expr 成立,返回结果 v1;否则,返回结果 v2 |
15 | IFNULL(v1,v2) | 如果 v1 的值不为 NULL,则返回 v1,否则返回 v2。 |
16 | ISNULL(expression) | 判断表达式是否为 NULL |
17 | LAST_INSERT_ID() | 返回最近生成的 AUTO_INCREMENT 值 |
18 | NULLIF(expr1, expr2) | 比较两个字符串,如果字符串 expr1 与 expr2 相等 返回 NULL,否则返回 expr1 |
19 | CASE expression WHEN condition1 THEN result1 WHEN condition2 THEN result2 … WHEN conditionN THEN resultN ELSE result END |
CASE 表示函数开始,END 表示函数结束。 如果 condition1 成立,则返回 result1, 如果 condition2 成立,则返回 result2, 当全部不成立则返回 result,而当有一个成立之后,后面的就不执行了。 |
Example: CONV(x,f1,f2) returns f1 base number to f2 base number
SELECT CONV(13,10,2);
result:
1101
8. Conclusion
It is really not easy to sort out these functions. Sometimes the things that have been sorted out through hard work are randomly picked up by some people with ulterior motives, so it is really difficult to be original. If you think this MySQL function notebook is really useful to you, I also hope to like, collect and support, thank you everyone!
Thank you for your reading and liking. I have collected a lot of technical dry goods, which can be shared with friends who like my articles. If you are willing to take the time to settle down and learn, they will definitely help you. The dry goods include:
Click on the business card at the end of the article to take it away