search
HomeDatabaseMysql TutorialMySQL中字符串函数详细介绍(1)

MySQL中字符串函数详细介绍(1)

Jun 07, 2016 pm 04:07 PM
mysqlstringintroducefunctionstringyesdetailed

符串或串(String)是由零个或多个字符组成的有限序列。一般记为 s='a1a2an'(n=0)。它是编程语言中表示文本的数据类型。 通常以串的整体作为操作对象,如:在串中查找某个子串、求取一个子串、在串的某个位置上插入一个子串以及删除一个子串等。两个字符串相等

符串或串(String)是由零个或多个字符组成的有限序列。一般记为 s='a1a2•••an'(n>=0)。它是编程语言中表示文本的数据类型。

通常以串的整体作为操作对象,如:在串中查找某个子串、求取一个子串、在串的某个位置上插入一个子串以及删除一个子串等。两个字符串相等的充要条件是:长度相等,并且各个对应位置上的字符都相等。设p、q是两个串,求q在p中首次出现的位置的运算叫做模式匹配。串的两种最基本的存储方式是顺序存储方式和链接存储方式。

下面我们来看看MySQL中的字符串函数

假如结果的长度大于 max_allowed_packet 系统变量的最大值时,字符串值函数的返回值为NULL。

对于在字符串位置操作的函数,第一个位置的编号为 1。

◆ ASCII(str)

返回值为字符串str 的最左字符的数值。假如str为空字符串,则返回值为 0 。假如str 为NULL,则返回值为 NULL。 ASCII()用于带有从 0到255的数值的字符。

mysql> SELECT ASCII('2');

        -> 50

mysql> SELECT ASCII(2);

        -> 50

mysql> SELECT ASCII('dx');

        -> 100

见 ORD()函数。

◆ BIN(N)

返回值为N的二进制值的字符串表示,其中  N 为一个longlong (BIGINT) 数字。这等同于 CONV(N,10,2)。假如N 为NULL,则返回值为 NULL。

mysql> SELECT BIN(12);<br>-> '1100'

◆ BIT_LENGTH(str)

返回值为二进制的字符串str 长度。

mysql> SELECT BIT_LENGTH('text');<br>-> 32

◆ CHAR(N,... [USING charset])

CHAR()将每个参数N理解为一个整数,其返回值为一个包含这些整数的代码值所给出的字符的字符串。NULL值被省略。

mysql> SELECT CHAR(77,121,83,81,'76');<br>-> 'MySQL'<br>mysql> SELECT CHAR(77,77.3,'77.3');<br>-> 'MMM'

大于 255的CHAR()参数被转换为多结果字符。 例如,CHAR(256) 相当于 CHAR(1,0), 而CHAR(256*256) 则相当于 CHAR(1,0,0):

mysql> SELECT HEX(CHAR(1,0)), HEX(CHAR(256));<br>+----------------+----------------+<br>| HEX(CHAR(1,0)) | HEX(CHAR(256)) |<br>+----------------+----------------+<br>| 0100           | 0100           |<br>+----------------+----------------+<br>mysql> SELECT HEX(CHAR(1,0,0)), HEX(CHAR(256*256));<br>+------------------+--------------------+<br>| HEX(CHAR(1,0,0)) | HEX(CHAR(256*256)) |<br>+------------------+--------------------+<br>| 010000           | 010000             |<br>+------------------+--------------------+

CHAR()的返回值为一个二进制字符串。可选择使用USING语句产生一个给出的字符集中的字符串:

mysql> SELECT CHARSET(CHAR(0x65)), CHARSET(CHAR(0x65 USING utf8));<br>mysql> SELECT CHARSET(CHAR(0x65)), CHARSET(CHAR(0x65 USING utf8));<br>+---------------------+--------------------------------+<br>| CHARSET(CHAR(0x65)) | CHARSET(CHAR(0x65 USING utf8)) |<br>+---------------------+--------------------------------+<br>| binary              | utf8                           |<br>+---------------------+--------------------------------+

如果 USING已经产生,而结果字符串不符合给出的字符集,则会发出警告。同样,如果严格的SQL模式被激活,则CHAR()的结果会成为 NULL。

◆ CHAR_LENGTH(str)

返回值为字符串str 的长度,长度的单位为字符。一个多字节字符算作一个单字符。对于一个包含五个二字节字符集, LENGTH()返回值为 10, 而CHAR_LENGTH()的返回值为5。

◆ CHARACTER_LENGTH(str)

CHARACTER_LENGTH()是CHAR_LENGTH()的同义词。

◆ COMPRESS(string_to_compress)

压缩一个字符串。这个函数要求 MySQL已经用一个诸如zlib的压缩库压缩过。   否则,返回值始终是NULL。UNCOMPRESS() 可将压缩过的字符串进行解压缩。

mysql> SELECT LENGTH(COMPRESS(REPEAT('a',1000)));<br>-> 21<br>mysql> SELECT LENGTH(COMPRESS(''));<br>-> 0<br>mysql> SELECT LENGTH(COMPRESS('a'));<br>-> 13<br>mysql> SELECT LENGTH(COMPRESS(REPEAT('a',16)));<br>-> 15

压缩后的字符串的内容按照以下方式存储:

空字符串按照空字符串存储。

非空字符串未压缩字符串的四字节长度进行存储(首先为低字节),后面是压缩字符串。如果字符串以空格结尾,就会在后加一个"."号,以防止当结果值是存储在CHAR或VARCHAR类型的字段列时,出现自动把结尾空格去掉的现象。(不推荐使用 CHAR 或VARCHAR 来存储压缩字符串。最好使用一个 BLOB 列代替)。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are stored procedures in MySQL?What are stored procedures in MySQL?May 01, 2025 am 12:27 AM

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

How does query caching work in MySQL?How does query caching work in MySQL?May 01, 2025 am 12:26 AM

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

What are the advantages of using MySQL over other relational databases?What are the advantages of using MySQL over other relational databases?May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.