Home Database Mysql Tutorial Summary of MySQL practical functions

Summary of MySQL practical functions

Dec 02, 2020 pm 04:51 PM
mysql

mysql video tutorial column introduces practical functions.

Summary of MySQL practical functions

Related free learning recommendations: mysql video tutorial

MySQL function encyclopedia and function explanation, you will definitely use it to manage MYSQL data.
Note: The subscript of mysql starts from 1

  1. ASCII(str)
    Return a string The ASCII code value of the leftmost character of str. If str is the empty string, 0 is returned. If str is NULL, return NULL.
mysql> select ASCII('2');
    -> 50
mysql> select ASCII(2);
    -> 50
mysql> select ASCII('dx');
    -> 100
Copy after login
  1. ORD(str)
    If the leftmost character of the string str is a multi-byte character, pass it in the format ((first byte ASCII code) 256 (second byte ASCII code))[256 third byte ASCII code...] returns the ASCII code value of the character to return the multi-byte character code. If the leftmost character is not a multibyte character. Returns the same value returned by the ASCII() function.
mysql> select ORD('2');
    -> 50
Copy after login
  1. CONV(N,from_base,to_base)
    Convert numbers between different base systems. Returns the string number of the number N, converted from the from_base base to the to_base base. If any parameter is NULL, returns NULL. Parameter N is interpreted as an integer, but can be specified as an integer or a string. The smallest base is 2 and the largest base is 36. If to_base is a negative number, N is considered a signed number, otherwise, N is considered an unsigned number. CONV Works with 64-bit precision.
    That is: N is the data to be converted, from_base is the original base, and to_base is the target base.
mysql> select CONV("a",16,2);
    -> '1010'
mysql> select CONV("6E",18,8);
    -> '172'
mysql> select CONV(-17,10,-18);
    -> '-H'
mysql> select CONV(10+"10"+'10'+0xa,10,10);
    -> '40'
Copy after login
  1. BIN(N)
    Returns a string representation of the binary value N, where N is a long integer ( BIGINT) number, which is equivalent to CONV(N,10,2). If N is NULL, return NULL.
mysql> select BIN(12);
    -> '1100'
Copy after login
  1. OCT(N)
    Returns a string representation of the octal value N, where N is a long integer. This is equivalent to At CONV(N,10,8). If N is NULL, return ``.
mysql> select OCT(12);
    -> '14'
Copy after login
  1. HEX(N)
    Returns the hexadecimal value NRepresentation of a string, where N is a Long integer (BIGINT) number, which is equivalent to CONV(N,10,16). If N is NULL, return NULL.
mysql> select HEX(255);
    -> 'FF'
Copy after login
  1. CHAR(N,...)
    CHAR()Interprets the parameters as integers and returns the values ​​of these integers ASCIIA string composed of code characters. NULL values ​​are skipped.
mysql> select CHAR(77,121,83,81,'76');
    -> 'MySQL'
mysql> select CHAR(77,77.3,'77.3');
    -> 'MMM'
Copy after login
  1. CONCAT(str1,str2,...)
    Returns the string from the parameter connection. If any argument is NULL, return NULL. Can have more than 2 parameters. A numeric argument is converted to its equivalent string form.
mysql> select CONCAT('My', 'S', 'QL');
    -> 'MySQL'
mysql> select CONCAT('My', NULL, 'QL');
    -> NULL
mysql> select CONCAT(14.3);
    -> '14.3'
Copy after login

9.LENGTH(str)OCTET_LENGTH(str)CHAR_LENGTH(str)CHARACTER_LENGTH( str)
Returns the length of the string str.
Note that for multi-byte characters, their CHAR_LENGTH() is only calculated once.

mysql> select LENGTH('text');
    -> 4
mysql> select LENGTH('简书');
    -> 6
mysql> select OCTET_LENGTH('text');
    -> 4
mysql> select OCTET_LENGTH('简书');
    -> 6
mysql> select CHAR_LENGTH('text');
    -> 4
mysql> select CHAR_LENGTH('简书');
    -> 2
mysql> select CHARACTER_LENGTH('text');
    -> 4
mysql> select CHARACTER_LENGTH('简书');
    -> 2
Copy after login
  1. LOCATE(substr,str)POSITION(substr IN str)
    Returns the first occurrence of substring substr in string str position, if substr is not in str, return 0.
mysql> select LOCATE('bar', 'foobarbar');
    -> 4
mysql> select LOCATE('xbar', 'foobar');
    -> 0
Copy after login

This function is multi-byte reliable.

  1. LOCATE(substr,str,pos)
    Returns the position where the substring substr first appears in the string str, starting from position pos. If substr is not inside str, return 0.
mysql> select LOCATE('bar', 'foobarbar',5);
    -> 7
Copy after login

This function is multi-byte reliable.

  1. INSTR(str,substr)
          返回子串substr在字符串str中的第一个出现的位置。这与有2个参数形式的LOCATE()相同,除了参数被颠倒。
mysql> select INSTR('foobarbar', 'bar');
    -> 4
mysql> select INSTR('xbar', 'foobar');
    -> 0
Copy after login
  1. LPAD(str,len,padstr)
          返回字符串str,左面用字符串padstr填补直到str是len个字符长。
mysql> select LPAD('hi',7,'abc');
    -> 'abcabhi'
Copy after login
  1. RPAD(str,len,padstr)
          返回字符串str,右面用字符串padstr填补直到str是len个字符长。
mysql> select RPAD('hi',7,'abc');
    -> 'hiabcab'
Copy after login
  1. LEFT(str,len)
          返回字符串str的最左面len个字符。
mysql> select LEFT('foobarbar', 5);
    -> 'fooba'
Copy after login
  1. RIGHT(str,len)
          返回字符串str的最右面len个字符。
mysql> select RIGHT('foobarbar', 4);
    -> 'rbar'
Copy after login
  1. SUBSTRING(str,pos,len)SUBSTRING(str FROM pos FOR len)MID(str,pos,len)
          从字符串str返回一个len个字符的子串,从位置pos开始。使用FROM的变种形式是ANSI SQL92语法。
mysql> select SUBSTRING('Quadratically',5,6);
    -> 'ratica'
mysql> select SUBSTRING('helloworld' FROM 2 FOR 5);
    -> 'ellow'
mysql> select MID('helloworld' FROM 2 FOR 5);
    -> 'ellow'
Copy after login

SUBSTR用法同SUBSTRING

  1. SUBSTRING_INDEX(str,delim,count)
          返回从字符串str的第count个出现的分隔符delim之后的子串。如果count是正数,返回最后的分隔符到左边(从左边数) 的所有字符。如果count是负数,返回最后的分隔符到右边的所有字符(从右边数)。
mysql> select SUBSTRING_INDEX('www.mysql.com', '.', 2);
    -> 'www.mysql'
mysql> select SUBSTRING_INDEX('www.mysql.com', '.', -2);
    -> 'mysql.com'
Copy after login
  1. LTRIM(str)
          返回删除了其前置空格字符的字符串str。
mysql> select LTRIM('     barbar');
    -> 'barbar'
Copy after login
  1. RTRIM(str)
          返回删除了其拖后空格字符的字符串str。
mysql> select RTRIM('barbar   ');
    -> 'barbar'
Copy after login
  1. TRIM([remstr FROM] str)TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)
          返回字符串str,其所有remstr前缀或后缀被删除了。如果没有修饰符BOTH、LEADING或TRAILING给出,BOTH被假定。如果remstr没被指定,空格被删除(中间空格不删除)。
 mysql> SELECT TRIM('  bar  bar   ');
         -> 'bar  bar'
 mysql> SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');   --删除指定的首字符 x
         -> 'barxxx'
 mysql> SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx');      --删除指定的首尾字符 x
         -> 'bar'
 mysql> SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');  --删除指定的尾字符 x
         -> 'barx'
Copy after login
  1. SOUNDEX(str)
          返回str的一个同音字符串。听起来“大致相同”的2个字符串应该有相同的同音字符串。一个“标准”的同音字符串长是4个字符,但是SOUNDEX()函数返回一个任意长的字符串。你可以在结果上使用SUBSTRING()得到一个“标准”的 同音串。所有非数字字母字符在给定的字符串中被忽略。所有在A-Z之外的字符国际字母被当作元音。
mysql> select SOUNDEX('Hello');
    -> 'H400'
mysql> select SOUNDEX('Quadratically');
    -> 'Q36324'
Copy after login
  1. SPACE(N)
          返回由N个空格字符组成的一个字符串。
mysql> select SPACE(6);
    -> '     '
Copy after login
  1. REPLACE(str,from_str,to_str)
          返回字符串str,其字符串from_str的所有出现由字符串to_str代替。
mysql> select REPLACE('www.mysql.com', 'w', 'Ww');
    -> 'WwWwWw.mysql.com'
Copy after login
  1. REPEAT(str,count)
          返回由重复countTimes次的字符串str组成的一个字符串。如果count <= 0,返回一个空字符串。如果str或count是NULL,返回NULL
mysql> select REPEAT('MySQL', 3);
    -> 'MySQLMySQLMySQL'<ol start="26"><li>
<code>REVERSE(str)</code><br>      返回颠倒字符顺序的字符串str。</li></ol>
<pre class="brush:php;toolbar:false">mysql> select REVERSE('abc');
    -> 'cba'
Copy after login
  1. INSERT(str,pos,len,newstr)
          返回字符串str,在位置pos起始的子串且len个字符长得子串由字符串newstr代替。
mysql> select INSERT('Quadratic', 3, 4, 'What');
    -> 'QuWhattic'
Copy after login
  1. ELT(N,str1,str2,str3,...)
          如果N= 1,返回str1,如果N= 2,返回str2,等等。如果N小于1或大于参数个数,返回NULLELT()FIELD()反运算。
mysql> select ELT(1, 'ej', 'Heja', 'hej', 'foo');
    -> 'ej'
mysql> select ELT(4, 'ej', 'Heja', 'hej', 'foo');
    -> 'foo'
Copy after login
  1. FIELD(str,str1,str2,str3,...)
          返回str在str1, str2, str3, ...清单的索引。如果str没找到,返回0。FIELD()ELT()反运算。
mysql> select FIELD('ej', 'Hej', 'ej', 'Heja', 'hej', 'foo');
    -> 2
mysql> select FIELD('fo', 'Hej', 'ej', 'Heja', 'hej', 'foo');
    -> 0
Copy after login
  1. 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('b','a,b,c,d');
   -> 2
Copy after login
  1. MAKE_SET(bits,str1,str2,...)
          返回一个集合 (包含由“,”字符分隔的子串组成的一个字符串),由相应的位在bits集合中的的字符串组成。str1对应于位0,str2对应位1,等等。在str1, str2, ...中的NULL串不添加到结果中。
mysql> SELECT MAKE_SET(1,'a','b','c');
    -> 'a'
mysql> SELECT MAKE_SET(1 | 4,'hello','nice','world');
    -> 'hello,world'
mysql> SELECT MAKE_SET(1 | 4,'hello','nice',NULL,'world');
-> 'hello'
mysql> SELECT MAKE_SET(0,'a','b','c');
    -> ''
Copy after login

说明
bits应将期转为二进制,如,1为,0001,倒过来排序,则为1000,将bits后面的字符串str1,str2等,放置在这个倒过来的二进制排序中,取出值为1对应的字符串,则得到hello.

1|4表示进行或运算,为0001 | 0100,得0101,倒过来排序,为1010,则'hello','nice','world'得到的是hello word。'hello','nice',NULL,'world'得到的是hello。NULL不取,只有1才取对应字符串.

  1. EXPORT_SET(bits,on,off,[separator,[number_of_bits]])
          返回一个字符串,在这里对于在“bits”中设定每一位,你得到一个“on”字符串,并且对于每个复位(reset)的位,你得到一个“off”字符串。每个字符串用“separator”分隔(缺省“,”),并且只有“bits”的“number_of_bits” (缺省64)位被使用。
mysql> select EXPORT_SET(5,'Y','N',',',4)
    -> Y,N,Y,N
Copy after login
  1. LCASE(str)LOWER(str)
          返回字符串str,根据当前字符集映射(缺省是ISO-8859-1 Latin1)把所有的字符改变成小写。该函数对多字节是可靠的。
mysql> select LCASE('QUADRATICALLY');
    -> 'quadratically'
Copy after login
  1. UCASE(str)UPPER(str)
          返回字符串str,根据当前字符集映射(缺省是ISO-8859-1 Latin1)把所有的字符改变成大写。该函数对多字节是可靠的。
mysql> select UCASE('Hello');
    -> 'HELLO'
Copy after login
  1. LOAD_FILE(file_name)
          读入文件并且作为一个字符串返回文件内容。文件必须在服务器上,你必须指定到文件的完整路径名,而且你必须有file权限。文件必须所有内容都是可读的并且小于max_allowed_packet。如果文件不存在或由于上面原因之一不能被读出,函数返回NULL
mysql> UPDATE table_name
      SET blob_column=LOAD_FILE("/tmp/picture")
      WHERE id=1;
Copy after login
  1. CONCAT(str1,str2,...)
          将多个字符串连接成一个字符串,返回结果为连接参数产生的字符串。如有任何一个参数为NULL,则返回值为 NULL。或许有一个或多个参数。 如果所有参数均为非二进制字符串,则结果为非二进制字符串。 如果自变量中含有任一二进制字符串,则结果为一个二进制字符串。一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型 cast, 例如: SELECT CONCAT(CAST(int_col AS CHAR), char_col)
mysql> SELECT CONCAT('My','S','ql');
    -> ‘MySQL’
mysql> SELECT CONCAT('My', NULL, 'ql');
    -> NULL
mysql> SELECT CONCAT(14.3);
    -> '14.3'
Copy after login

MySQL必要时自动变换数字为字符串,并且反过来也如此:

mysql> SELECT 1+"1";
    -> 2
mysql> SELECT CONCAT(2,' test');
    -> '2 test'
Copy after login
  1. CONCAT_WS(separator,str1,str2,…)
          CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。   第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为NULL,则结果为NULL。函数会忽略任何分隔符参数后的 NULL 值。
mysql>select CONCAT_WS(',','First name','Second name','Last Name');
    -> 'First name,Second name,Last Name'
mysql> select CONCAT_WS(',','First name',NULL,'Last Name');
    -> 'First name,Last Name'
Copy after login

 CONCAT_WS()不会忽略任何空字符串。 (然而会忽略所有的 NULL)。

The above is the detailed content of Summary of MySQL practical functions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

How to recover data after SQL deletes rows How to recover data after SQL deletes rows Apr 09, 2025 pm 12:21 PM

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

See all articles