mysql 函数

本文深入探讨了SQL中用于文本与字符串操作的主要函数,包括ASCII、BIN、BIT_LENGTH等基本函数以及更复杂的如 CONCAT_WS、REPLACE、REVERSE等高级功能,通过实例演示了如何在数据库查询中灵活运用这些函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数名称描述 
ASCII()Return numeric value of left-most character 
BIN()Return a string containing binary representation of a number 
BIT_LENGTH()Return length of argument in bits 
CHAR_LENGTH()Return number of characters in argument 
CHAR()Return the character for each integer passed 
CHARACTER_LENGTH()Synonym for CHAR_LENGTH() 
CONCAT_WS()Return concatenate with separator 
CONCAT()Return concatenated string 
ELT()Return string at index number 
EXPORT_SET()Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string 
FIELD()Return the index (position) of the first argument in the subsequent arguments 
FIND_IN_SET()Return the index position of the first argument within the second argument 
FORMAT()Return a number formatted to specified number of decimal places 
HEX()Return a hexadecimal representation of a decimal or string value 
INSERT()Insert a substring at the specified position up to the specified number of characters 
INSTR()Return the index of the first occurrence of substring 
LCASE()Synonym for LOWER() 
LEFT()Return the leftmost number of characters as specified 
LENGTH()Return the length of a string in bytes 
LIKESimple pattern matching 
LOAD_FILE()Load the named file 
LOCATE()Return the position of the first occurrence of substring 
LOWER()Return the argument in lowercase 
LPAD()Return the string argument, left-padded with the specified string 
LTRIM()Remove leading spaces 
MAKE_SET()Return a set of comma-separated strings that have the corresponding bit in bits set 
MATCHPerform full-text search 
MID()Return a substring starting from the specified position 
NOT LIKENegation of simple pattern matching 
NOT REGEXPNegation of REGEXP 
OCT()Return a string containing octal representation of a number 
OCTET_LENGTH()Synonym for LENGTH() 
ORD()Return character code for leftmost character of the argument 
POSITION()Synonym for LOCATE() 
QUOTE()Escape the argument for use in an SQL statement 
REGEXPPattern matching using regular expressions 
REPEAT()Repeat a string the specified number of times 
REPLACE()Replace occurrences of a specified string 
REVERSE()Reverse the characters in a string 
RIGHT()Return the specified rightmost number of characters 
RLIKESynonym for REGEXP 
RPAD()Append string the specified number of times 
RTRIM()Remove trailing spaces 
SOUNDEX()Return a soundex string 
SOUNDS LIKECompare sounds 
SPACE()Return a string of the specified number of spaces 
STRCMP()Compare two strings 
SUBSTR()Return the substring as specified 
SUBSTRING_INDEX()Return a substring from a string before the specified number of occurrences of the delimiter 
SUBSTRING()Return the substring as specified 
TRIM()Remove leading and trailing spaces 
UCASE()Synonym for UPPER() 
UNHEX()Return a string containing hex representation of a number 
UPPER()Convert to uppercase 

1 函数 ASCII() 返回字符的ASCII值

mysql> select ascii('a');
+------------+
| ascii('a') |
+------------+
|         97 |
+------------+
1 row in set

mysql> select ascii('b');
+------------+
| ascii('b') |
+------------+
|         98 |
+------------+
1 row in set

 2 bin()函数获得某数字的二进制

mysql> select bin(1);
+--------+
| bin(1) |
+--------+
| 1      |
+--------+
1 row in set

mysql> select bin(3);
+--------+
| bin(3) |
+--------+
| 11     |
+--------+
1 row in set

 

BIT_LENGTH() 获得某参数的比特位数

 

mysql> select bit_length('a');
+-----------------+
| bit_length('a') |
+-----------------+
|               8 |
+-----------------+
1 row in set
mysql> select bit_length('ab');
+------------------+
| bit_length('ab') |
+------------------+
|               16 |
+------------------+
1 row in set

 4 CHAR_LENGTH() 获得字符长度

 

 mysql> select char_length('a');
+------------------+
| char_length('a') |
+------------------+
|                1 |
+------------------+
1 row in set
mysql> select char_length(12);
+-----------------+
| char_length(12) |
+-----------------+
|               2 |
+-----------------+
1 row in set

  5 CONCAT_WS() 拼接字符串

 

mysql> select concat_ws('-','a','b');
+------------------------+
| concat_ws('-','a','b') |
+------------------------+
| a-b                    |
+------------------------+
1 row in set
mysql> select concat_ws(',','aa','bb','cc');
+-------------------------------+
| concat_ws(',','aa','bb','cc') |
+-------------------------------+
| aa,bb,cc                      |
+-------------------------------+
1 row in set

 6 CONCAT() 直接拼接字符串

  

mysql> select concat('aa','bb');
+-------------------+
| concat('aa','bb') |
+-------------------+
| aabb              |
+-------------------+
1 row in set

mysql> select concat('a','c','b');
+---------------------+
| concat('a','c','b') |
+---------------------+
| acb                 |
+---------------------+
1 row in set

 7 CHAR() 将参数解释为整数并且返回由这些整数的ascii代码字符组成的一个字符串。null值被跳过。

 

mysql> select char(77);
+----------+
| char(77) |
+----------+
| M        |
+----------+
1 row in set
 8  field(str,str1,str2,str3,...)  

 

 

mysql> select field('ee','eaa','eee','ee');
+------------------------------+
| field('ee','eaa','eee','ee') |
+------------------------------+
|                            3 |
+------------------------------+
1 row in set
 9  find_in_set(str,strlist)  

 

如果字符串str在由n子串组成的表strlist之中,返回一个1到n的值。一个字符串表是被“,”分隔的子串组成的一个字符串。如果第一个参数是一个常数字符串并且第二个参数是一种类型为set的列,find_in_set()函数被优化而使用位运算!如果str不是在strlist里面或如果strlist是空字符串,返回0。如果任何一个参数是null,返回null。如果第一个参数包含一个“,”,该函数将工作不正常。
mysql> select find_in_set('bb','a,b,c,d,e,bb,ee');
+-------------------------------------+
| find_in_set('bb','a,b,c,d,e,bb,ee') |
+-------------------------------------+
|                                   6 |
+-------------------------------------+
1 row in set
 10  format()函数在mysql中是数据内容格式化的
mysql> select format(23.5666,3);
+-------------------+
| format(23.5666,3) |
+-------------------+
| 23.567            |
+-------------------+
1 row in set
 11  LCASE 转换成小写
mysql> select LCASE('AA');
+-------------+
| LCASE('AA') |
+-------------+
| aa          |
+-------------+
1 row in set
 12 LEFT(str,len)  返回字符串str的最左面len个字符。
mysql> select left('adfdfeedf',3);
+---------------------+
| left('adfdfeedf',3) |
+---------------------+
| adf                 |
+---------------------+
1 row in set
 13  类似subString,用于截取字符串
mysql> select mid('abcdefg',2,4);
+--------------------+
| mid('abcdefg',2,4) |
+--------------------+
| bcde               |
+--------------------+
1 row in set
 13 REPEAT  返回由重复 count Times次的字符串 str 组成的一个字符串。如果 count <= 0 ,返回一个空字符串。如果 str count NULL ,返回 NULL
mysql> select repeat('ab',5);
+----------------+
| repeat('ab',5) |
+----------------+
| ababababab     |
+----------------+
1 row in set
 
 
 
### MYSQL 函数概述 在 MySQL 数据库管理系统中,函数是一类重要的工具,可以用来执行各种计算、转换和其他操作。这些函数分为内置函数和自定义函数两大类。以下是常见的 MySQL 内置函数分类及其具体用法。 --- #### **1. 字符串函数** 字符串函数主要用于处理字符型数据,支持拼接、截取、替换等功能。 - **`CONCAT(str1, str2, ...)`**: 将多个字符串连接成一个字符串。 ```sql SELECT CONCAT('Hello', ' ', 'World'); -- 返回 "Hello World" ``` - **`SUBSTRING(str, pos, len)`**: 提取子字符串,从 `pos` 开始提取长度为 `len` 的部分。 ```sql SELECT SUBSTRING('MySQL Function', 7, 8); -- 返回 "Function" ``` - **`REPLACE(str, from_str, to_str)`**: 替换字符串中的部分内容。 ```sql SELECT REPLACE('Database Management System', 'Management', 'Admin'); -- 返回 "Database Admin System" ``` - **`UPPER(str)` 和 `LOWER(str)`**: 转换字符串大小写。 ```sql SELECT UPPER('mysql'), LOWER('MYSQL'); -- 返回 "MYSQL", "mysql" ``` 以上内容来源于常见字符串函数介绍[^2]。 --- #### **2. 日期时间函数** 日期时间函数用于处理与时间和日期相关的数据。 - **`NOW()`**: 获取当前的日期和时间。 ```sql SELECT NOW(); -- 返回类似于 "2023-10-05 14:30:00" ``` - **`DATE(date_time_expr)`**: 提取日期部分。 ```sql SELECT DATE('2023-10-05 14:30:00'); -- 返回 "2023-10-05" ``` - **`DATE_FORMAT(date, format)`**: 根据指定格式化模板返回日期/时间字符串。 ```sql SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s'); -- 自定义格式输出 ``` 相关内容可参见日期函数说明[^2]。 --- #### **3. 数学函数** 数学函数提供了基本的数值运算能力。 - **`ABS(num)`**: 计算绝对值。 ```sql SELECT ABS(-10); -- 返回 10 ``` - **`ROUND(num, decimals)`**: 对数字四舍五入到指定位数的小数。 ```sql SELECT ROUND(123.456, 2); -- 返回 123.46
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值