MySQL中字符串函数详细介绍(1)
符串或串(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 Copy after login |
见 ORD()函数。
◆ BIN(N)
返回值为N的二进制值的字符串表示,其中 N 为一个longlong (BIGINT) 数字。这等同于 CONV(N,10,2)。假如N 为NULL,则返回值为 NULL。
mysql> SELECT BIN(12);<br>-> '1100' Copy after login |
◆ BIT_LENGTH(str)
返回值为二进制的字符串str 长度。
mysql> SELECT BIT_LENGTH('text');<br>-> 32 Copy after login |
◆ 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' Copy after login |
大于 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>+------------------+--------------------+ Copy after login |
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>+---------------------+--------------------------------+ Copy after login |
如果 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 Copy after login |
压缩后的字符串的内容按照以下方式存储:
空字符串按照空字符串存储。
非空字符串未压缩字符串的四字节长度进行存储(首先为低字节),后面是压缩字符串。如果字符串以空格结尾,就会在后加一个"."号,以防止当结果值是存储在CHAR或VARCHAR类型的字段列时,出现自动把结尾空格去掉的现象。(不推荐使用 CHAR 或VARCHAR 来存储压缩字符串。最好使用一个 BLOB 列代替)。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

2024 is the first year of AI mobile phones. More and more mobile phones integrate multiple AI functions. Empowered by AI smart technology, our mobile phones can be used more efficiently and conveniently. Recently, the Galaxy S24 series released at the beginning of the year has once again improved its generative AI experience. Let’s take a look at the detailed function introduction below. 1. Generative AI deeply empowers Samsung Galaxy S24 series, which is empowered by Galaxy AI and brings many intelligent applications. These functions are deeply integrated with Samsung One UI6.1, allowing users to have a convenient intelligent experience at any time, significantly improving the performance of mobile phones. Efficiency and convenience of use. The instant search function pioneered by the Galaxy S24 series is one of the highlights. Users only need to press and hold
