Home Database Mysql Tutorial Three types of commonly used functions in mysql

Three types of commonly used functions in mysql

May 16, 2018 pm 03:53 PM
mysql function use

This article mainly introduces the three types of functions commonly used in MySQL. Interested friends can refer to it. I hope it will be helpful to everyone.

1. String class.

Note: When mysql processes strings, character subscripts start from 1.

1. concat(string1, string2, ...); //Connect string

mysql> select concat('leng', 'xue', 'gang') as name;
-------------
| name |
-------------
| lengxuegang |
-------------
1 row in set (0.00 sec)

2. instr(string, substring); //Return the position where substring first appears in string , does not exist and returns 0

mysql> select instr('lengxuegang', 'xue');
----------------------- -------
| instr('lengxuegang', 'xue') |
----------------------- ----
|                                                                        ​0.00 sec)

mysql> select instr('lengxuegang', 'none');
--------------------- ------
| instr('lengxuegang', 'none') |
-------------------------- ----
|               0 |
---------------------------------
1 row in set (0.00 sec)

3、lcase(string); //Convert to lowercase

mysql> select lcase('LengxueGang');

--------- -------------

| lcase('LengxueGang') |
----------------------
| lengxuegang |
----------------------
1 row in set (0.00 sec)

4、left (string, length); //Take length characters from the left side of string

mysql> select left('lengxuegang', 4);

------------- ----------

| left('lengxuegang', 4) |
----------------------- -
| length


#5、length(string); //Return the length of string

mysql> select length('lengxuegang');

-------- ---------------
| length('lengxuegang') |

-------------------------- --

|           11 |

-----------------------

1 row in set (0.25 sec)

6. locate(substring, string, [start_position]); //Start the search from start_position and return the position where substring first appears in string. Its function is similar to instr, but note that the positions of string and substring are different.

mysql> select locate('leng', 'lengxueganglengxuegang', 4);
-------------------------- --------------------
| locate('leng', 'lengxueganglengxuegang', 4) |

---------- -----------------------------------

| ---------------------------------------

1 row in set (0.00 sec)


7. ltrim(string); //Remove the spaces on the left

mysql> select ltrim(' leng');
------------- ------
| ltrim(' leng') |
------------------

| leng |

--- ---------------

1 row in set (0.00 sec)


8、repeat(string, count); //Repeat string count times

mysql> select repeat('leng', 4);
-------------------
| repeat('leng', 4) |
-------------------

| langlenglengleng |

-------------------

1 row in set (0.00 sec)


9、replace(string, search_str, replace_str); //Replace search_str with replace_str in string

mysql> select replace(' lengxueganglengxuegang', 'leng', 'cheng');
---------------------------------- ------------------
| replace('lengxueganglengxuegang', 'leng', 'cheng') |
----------- ----------------------------------------

| chengxuegangchengxuegang                                        -------------------------------------------------- --

1 row in set (0.05 sec)

10, rtrim(string); //Remove right-end spaces

mysql> select rtrim('leng ');
--------------------------
| rtrim('leng ') |
--------------------
| leng                                                                            ##1 row in set (0.00 sec)

11, strcmp(string1, string2); //Compare the sizes of two strings and return 1, 0, -1 respectively according to the size relationship

mysql> select strcmp('leng', 'cheng');

-----------------------

| strcmp(' leng', 'cheng') |
----------------------
| --------------------
1 row in set (0.04 sec)

mysql> select strcmp('cheng', 'leng') ;
-------------------------
| strcmp('cheng', 'leng') |
--- -----------------------
|                 -1 | -------
1 row in set (0.00 sec)

mysql> select strcmp('leng', 'leng');
--------- ---------------
| strcmp('leng', 'leng') |
--------------- -------
|               0 |
-----------------------------
1 row in set (0.00 sec )

12. substring(string, start_pos, length); //Start from start_pos of string, take length characters

mysql> select substring('lengxuegang', 5, 3);

--------------------------------

| substring('lengxuegang', 5, 3) |

--------------------------------

| --------------------------
1 row in set (0.00 sec)

13、trim(); / /Remove spaces at both ends of the string

mysql> select trim(' leng ');
-------------------

| trim (' leng ') |

-------------------

| leng |

------------- ------
1 row in set (0.00 sec)

14、ucase(string); //Convert to uppercase

mysql> select ucase('lengxuegang') ;
-----------------------

| ucase('lengxuegang') |

---------- ------------

| LENGXUEGANG |

----------------------
1 row in set (0.00 sec)

15.right(string, length); //Get length characters from the right side of string

mysql> select right('lengxuegang', 4);
-- -----------------------

| right('lengxuegang', 4) |

---------- ----------------

| gang                                                              #1 row in set (0.00 sec)


16, space(count); //Generate count spaces

mysql> select space(5);
----- -----
| space(5) |
----------

| |

----------

1 row in set (0.00 sec)


17, lpad(string, length, pad); //Padding pad at the left end of string until its length reaches length

mysql> select lpad('leng ', 10, 'dacb');
--------------------------
| lpad('leng', 10, 'dacb') |
--------------------------

| dacbdaleng |

------- ------------------

1 row in set (0.00 sec)


18, rpad(); //Fill pad at the right end of string , until its length reaches length

mysql> select rpad('leng', 10, 'dacb');
------------------- -------
| rpad('leng', 10, 'dacb') |
----------------------- ---

| lengdacbda |

--------------------------

1 row in set (0.00 sec)


19. coalesce(value1, value2, ...) returns the first non-null value. If all are null, return null

mysql> select coalesce(null, 1, 2) ;
----------------------
| coalesce(null, 1, 2) |
-------- -------------

|           1 | in set (0.03 sec)

2. Mathematics


1. abs(num); //Return absolute value

mysql> select abs(-3.5);
-----------
| abs(-3.5) |
-----------
| 3.5 |
-----------
1 row in set (0.03 sec)

2, bin(decimal_num); //Convert decimal to binary

mysql> select bin(12);
---------
| bin(12) |
---------
| 1100 |
---------
1 row in set (0.05 sec)

3、ceiling(num); //Round up

mysql> ; select ceiling(3.4);
--------------
| ceiling(3.4) |
--------------
|       4 |
--------------
1 row in set (0.00 sec)

mysql> select ceiling(-3.4);
---------------
| ceiling(-3.4) |
---------------
| 3 |
---------------
1 row in set (0.00 sec)

4、conv(num, from_base, to_base); // Base conversion

mysql> select conv(10, 10, 2);
-----------------
| conv(10, 10 , 2) |
-----------------
| 1010 |
-----------------
1 row in set (0.00 sec)

5、floor(num); //Round down

mysql> select floor(3.6);

------------
| floor(3.6) |
------------
| 3 |
-- ----------
1 row in set (0.00 sec)

mysql> select floor(-3.6);
----------- --
| floor(-3.6) |
-------------
| -4 |
------------- -

1 row in set (0.00 sec)

6、least(num1, num2, num3, ...); //Take the minimum value

mysql> select least(10, 4, -4, 0);
---------------------
| least(10, 4, -4, 0) |
---------------------
| -4 |
-------- -------------
1 row in set (0.10 sec)

7、mod(); //Take remainder

mysql> select mod (10, 3);
----------------
| mod(10, 3) |
----------------
|       1 |
----------------
1 row in set (0.00 sec)

8、power(num, power); //Power operation

mysql> select power(3, 3);
-------------
| power(3, 3) |
--- ----------
| 27 |
-------------
1 row in set (0.08 sec)

9 , rand([seed]); //Random number

mysql> select rand();
------------------
| rand() |
------------------
| 0.10342728263086 |
---------------- --
1 row in set (0.00 sec)

mysql> select rand();
------------------
| rand() |
------------------
| 0.98467650821868 |
--------------- ---
1 row in set (0.00 sec)

10, round(number, [decimals]); //Rounding, decimals is the number of decimal places

mysql> select round (1.2345);
---------------
| round(1.2345) |
---------------
|       1 |
---------------
1 row in set (0.00 sec)

mysql> select round(1.2345, 3);
------------------
| round(1.2345, 3) |
--------------- ---
| 1.235 |
------------------
1 row in set (0.00 sec)

11、sign (number); //Return sign, positive or negative or 0

mysql> select sign(0);
---------
| sign(0) |
---------
| 0 |
---------
1 row in set (0.00 sec)

mysql> select sign( 2);
---------
| sign(2) |
---------
| 1 |
----- ----
1 row in set (0.00 sec)

mysql> select sign(-2);
----------
| sign(- 2) |
----------
| -1 |
----------
1 row in set (0.00 sec)

12, sqrt(num); //square root

mysql> select sqrt(3);
-----------------
| sqrt(3) |
--------------
| 1.7320508075689 |
-------------- ---
1 row in set (0.00 sec)

13、greatest(value1, value2, ...); //Take the maximum value

mysql> select greatest(2 , 3, 10);
--------------------
| greatest(2, 3, 10) |
----- ---------------
|           10 |
--------------------
1 row in set (0.00 sec)

3. Date and time class

1. current_date(); //Return the current date

mysql> select current_date();
-------------
| current_date() |
----------------
| 2012-07-01 |
---------------- --
1 row in set (0.04 sec)

2、current_time(); //Return the current time

mysql> select current_time();
---- ------------
| current_time() |
-------------
| 02:05:41 |
----------------
1 row in set (0.00 sec)

3. current_timestamp(); //Return the current timestamp

mysql> select current_timestamp();
---------------------
| current_timestamp() |
----- ----------------
| 2012-07-01 02:06:12 |
---------------- -----
1 row in set (0.04 sec)

4、now(); //Return the current time

mysql> select now();
- --------------------
| now()                                                                                --
| 2012-07-01 02:06:57 |
---------------------
1 row in set (0.00 sec)

Related recommendations:

Recommended MySQL common function benefits

MySQL common functions in PHP are necessary to operate the database under PHP

Common MYSQL functions in PHP (necessary for operating databases under PHP)_PHP tutorial

The above is the detailed content of Three types of commonly used functions in mysql. 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

Video Face Swap

Video Face Swap

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

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: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

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

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

See all articles