MySQL: mysql functions

MySQL: mysql functions

1. Built-in functions

1. Mathematical functions

rand() round(num) ceil(num) floor(num)
random rounding Round up Round down

2. String functions

length() Byte length
char_length() Character length

ucase() Uppercase
lcase() lowercase

concat(character,...,character n) concatenates strings

replace(string, old character, new character) string replacement

Intercepting a string
left (string, truncated length)
right (string, truncated length)
substring(string, start position, truncation length) #includes the start position

mysql> select left('123456',4);
+------------------+
| left('123456',4) |
+------------------+
| 1234 |
+------------------+
1 row in set (0.00 sec)

mysql> select right('123456',4);
+-------------------+
| right('123456',4) |
+-------------------+
| 3456 |
+-------------------+
1 row in set (0.00 sec)

mysql> select substring('123456',2,4); 
+-------------------------+
| substring('123456',2,4) |
+-------------------------+
| 2345 |
+-------------------------+
1 row in set (0.00 sec)

3. Date functions

now() unix_timestamp() from_unixtime()
Current time Timestamp Formatting timestamps

mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2019-03-16 14:55:42 |
+---------------------+
1 row in set (0.00 sec)

mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
|1552719356 |
+------------------+
1 row in set (0.00 sec)

mysql> select from_unixtime(1552719356);
+---------------------------+
| from_unixtime(1552719356) |
+---------------------------+
| 2019-03-16 14:55:56 |
+---------------------------+
1 row in set (0.00 sec)

year() month() day() hour() minute() second()
Year moon day hour point Second

mysql> select 
 -> year(now()) as 'year',
 -> month(now()) as 'month',
 -> day(now()) as '日',
 -> hour(now()) as 'hour',
 -> minute(now()) as '分',
 -> second(now()) as 'seconds';
+------+------+------+------+------+------+
| Year| Month| Day| Hour| Minute| Second|
+------+------+------+------+------+------+
| 2019 | 3 | 16 | 14 | 59 | 12 |
+------+------+------+------+------+------+

4. Encryption Function

md5(data)
password(data)

5. Conditional judgment function

1). Syntax: if (data, value1, value2) #Judge whether the specified data is true: true - value1, false - value2

mysql> select if(null,1,2);
+--------------+
| if(null,1,2) |
+--------------+
| 2 |
+--------------+
1 row in set (0.00 sec)

mysql> select if(1,0,2);
+-----------+
| if(1,0,2) |
+-----------+
| 0 |
+-----------+
1 row in set (0.00 sec)

2). Syntax: IFNULL(data, value2) #Judge whether the specified data is null: null-value2, non-null-itself

mysql> select ifnull(0,123);
+---------------+
| ifnull(0,123) |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec)

mysql> select ifnull('a',123);
+-----------------+
| ifnull('a',123) |
+-----------------+
| a |
+-----------------+
1 row in set (0.00 sec)

2. Custom functions

grammar:

#Modify the end character delimiter //
create function function name (parameter name type, ..., parameter name n type n) returns return data type begin
#SQL statement return return value;
end //
delimiter ;

#Call select function name();

Output "hello world" (function without parameters)

#Judge whether the function exists, if it exists, delete it drop function if exists f1;

delimiter //
create function f1() returns varchar(30)
begin
 return 'hello world';
end //
delimiter ;

select f1();

+-------------+
| f1() |
+-------------+
| hello world |
+-------------+

Pass two integers to sum (function with parameters)

drop function if exists f2;

delimiter //

create function f2(num1 int, num2 int) returns int
begin
  return num1 + num2;
end //
delimiter ;

select f2(8, 2);

+----------+
| f2(8, 2) |
+----------+
| 10 |
+----------+

3. Custom function related syntax

Show all functions: show function status\G #The output is very large

Delete function: drop function [if exists] function name;

4. The difference between stored procedures and functions

Stored procedures can return multiple values, while custom functions can only return one value.

Stored procedures are generally executed independently, while functions are often used as part of other SQL statements.

The above is what I introduced to you: Database_MySQL: Detailed explanation and integration of MySQL functions. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Explanation of the usage scenarios of sql and various nosql databases
  • Detailed explanation of database language classification DDL, DCL, DML
  • Explanation of CAST and CONVERT functions for type conversion in MySQL database
  • MySQL 8.0.15 installation graphic tutorial and database basics
  • SQL SERVER database backup code example
  • Syntax and usage of window functions in PostgreSQL database
  • How to reasonably use the redundant fields of the database
  • Mysql master/slave database synchronization configuration and common errors
  • PHP singleton mode database connection class and page static implementation method
  • How to improve query efficiency in a database query of tens of millions?

<<:  The problem of mmx64.efi not found occurs when installing Ubuntu18 dual system on win10

>>:  Vue uses three methods to refresh the page

Recommend

How to modify the group to which a user belongs in Linux

Modify the group to which a user belongs in Linux...

Detailed example of MySQL (5.6 and below) parsing JSON

MySQL (5.6 and below) parses json #json parsing f...

Detailed tutorial for downloading and installing mysql8.0.21

Official website address: https://www.mysql.com/ ...

Example of how to quickly build a LEMP environment with Docker

LEMP (Linux + Nginx + MySQL + PHP) is basically a...

JavaScript source code for Elimination

JavaScript to achieve the source code download ad...

Linux disk management LVM usage

1. Introduction to LVM When we manage Linux disks...

Use ab tool to perform API stress test on the server

Table of contents 1 A brief introduction to syste...

How to install mongodb 4.2 using yum on centos8

1. Make a repo file Refer to the official install...

CentOS7 installation GUI interface and remote connection implementation

Use the browser (webdriver)-based selenium techno...

Summary of MySQL string interception related functions

This article introduces MySQL string interception...

SQL implementation LeetCode (176. Second highest salary)

[LeetCode] 176. Second Highest Salary Write a SQL...

React implements a general skeleton screen component example

Table of contents What is a skeleton screen? Demo...