mysql video tutorial column introduces 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
-
ASCII(str)
Return a string TheASCII
code value of the leftmost character of str. If str is the empty string, 0 is returned. If str isNULL
, returnNULL
.
mysql> select ASCII('2'); -> 50 mysql> select ASCII(2); -> 50 mysql> select ASCII('dx'); -> 100
-
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 theASCII
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 theASCII()
function.
mysql> select ORD('2'); -> 50
-
CONV(N,from_base,to_base)
Convert numbers between different base systems. Returns the string number of the numberN
, converted from the from_base base to the to_base base. If any parameter isNULL
, returnsNULL
. ParameterN
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, andto_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'
-
BIN(N)
Returns a string representation of the binary value N, whereN
is a long integer (BIGINT
) number, which is equivalent toCONV(N,10,2)
. IfN
isNULL
, returnNULL
.
mysql> select BIN(12); -> '1100'
-
OCT(N)
Returns a string representation of the octal value N, where N is a long integer. This is equivalent to AtCONV(N,10,8)
. IfN
isNULL
, return ``.
mysql> select OCT(12); -> '14'
-
HEX(N)
Returns the hexadecimal valueN
Representation of a string, where N is a Long integer (BIGINT
) number, which is equivalent toCONV(N,10,16)
. IfN
isNULL
, returnNULL
.
mysql> select HEX(255); -> 'FF'
-
CHAR(N,...)
CHAR()
Interprets the parameters as integers and returns the values of these integersASCII
A 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'
-
CONCAT(str1,str2,...)
Returns the string from the parameter connection. If any argument isNULL
, returnNULL
. 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'
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
-
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
This function is multi-byte reliable.
-
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
This function is multi-byte reliable.
-
INSTR(str,substr)
返回子串substr在字符串str中的第一个出现的位置。这与有2个参数形式的LOCATE()相同,除了参数被颠倒。
mysql> select INSTR('foobarbar', 'bar'); -> 4 mysql> select INSTR('xbar', 'foobar'); -> 0
-
LPAD(str,len,padstr)
返回字符串str,左面用字符串padstr填补直到str是len个字符长。
mysql> select LPAD('hi',7,'abc'); -> 'abcabhi'
-
RPAD(str,len,padstr)
返回字符串str,右面用字符串padstr填补直到str是len个字符长。
mysql> select RPAD('hi',7,'abc'); -> 'hiabcab'
-
LEFT(str,len)
返回字符串str的最左面len个字符。
mysql> select LEFT('foobarbar', 5); -> 'fooba'
-
RIGHT(str,len)
返回字符串str的最右面len个字符。
mysql> select RIGHT('foobarbar', 4); -> 'rbar'
-
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'
注: SUBSTR
用法同SUBSTRING
-
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'
-
LTRIM(str)
返回删除了其前置空格字符的字符串str。
mysql> select LTRIM(' barbar'); -> 'barbar'
-
RTRIM(str)
返回删除了其拖后空格字符的字符串str。
mysql> select RTRIM('barbar '); -> 'barbar'
-
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'
-
SOUNDEX(str)
返回str的一个同音字符串。听起来“大致相同”的2个字符串应该有相同的同音字符串。一个“标准”的同音字符串长是4个字符,但是SOUNDEX()
函数返回一个任意长的字符串。你可以在结果上使用SUBSTRING()
得到一个“标准”的 同音串。所有非数字字母字符在给定的字符串中被忽略。所有在A-Z之外的字符国际字母被当作元音。
mysql> select SOUNDEX('Hello'); -> 'H400' mysql> select SOUNDEX('Quadratically'); -> 'Q36324'
-
SPACE(N)
返回由N
个空格字符组成的一个字符串。
mysql> select SPACE(6); -> ' '
-
REPLACE(str,from_str,to_str)
返回字符串str,其字符串from_str的所有出现由字符串to_str代替。
mysql> select REPLACE('www.mysql.com', 'w', 'Ww'); -> 'WwWwWw.mysql.com'
-
REPEAT(str,count)
返回由重复countTimes次的字符串str组成的一个字符串。如果count NULL,返回NULL
。
mysql> select REPEAT('MySQL', 3); -> 'MySQLMySQLMySQL'
-
REVERSE(str)
返回颠倒字符顺序的字符串str。
mysql> select REVERSE('abc'); -> 'cba'
-
INSERT(str,pos,len,newstr)
返回字符串str,在位置pos起始的子串且len个字符长得子串由字符串newstr代替。
mysql> select INSERT('Quadratic', 3, 4, 'What'); -> 'QuWhattic'
-
ELT(N,str1,str2,str3,...)
如果N= 1
,返回str1,如果N= 2
,返回str2,等等。如果N
小于1或大于参数个数,返回NULL
。ELT()
是FIELD()
反运算。
mysql> select ELT(1, 'ej', 'Heja', 'hej', 'foo'); -> 'ej' mysql> select ELT(4, 'ej', 'Heja', 'hej', 'foo'); -> 'foo'
-
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
-
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
-
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'); -> ''
说明:
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才取对应字符串.
-
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
-
LCASE(str)
、LOWER(str)
返回字符串str,根据当前字符集映射(缺省是ISO-8859-1 Latin1)把所有的字符改变成小写。该函数对多字节是可靠的。
mysql> select LCASE('QUADRATICALLY'); -> 'quadratically'
-
UCASE(str)
、UPPER(str)
返回字符串str,根据当前字符集映射(缺省是ISO-8859-1 Latin1)把所有的字符改变成大写。该函数对多字节是可靠的。
mysql> select UCASE('Hello'); -> 'HELLO'
-
LOAD_FILE(file_name)
读入文件并且作为一个字符串返回文件内容。文件必须在服务器上,你必须指定到文件的完整路径名,而且你必须有file权限。文件必须所有内容都是可读的并且小于max_allowed_packet。如果文件不存在或由于上面原因之一不能被读出,函数返回NULL
。
mysql> UPDATE table_name SET blob_column=LOAD_FILE("/tmp/picture") WHERE id=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'
MySQL必要时自动变换数字为字符串,并且反过来也如此:
mysql> SELECT 1+"1"; -> 2 mysql> SELECT CONCAT(2,' test'); -> '2 test'
-
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'
注 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!

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function