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 use crontab to add scheduled tasks in Linux

Preface The Linux system is controlled by the sys...

CSS3 realizes the animation effect of lotus blooming

Let’s look at the effect first: This effect looks...

Comprehensive understanding of html.css overflow

Comprehensive understanding of html.css overflow ...

Linux firewall status check method example

How to check the status of Linux firewall 1. Basi...

Vue component encapsulates sample code for uploading pictures and videos

First download the dependencies: cnpm i -S vue-uu...

Highly recommended! Setup syntax sugar in Vue 3.2

Table of contents Previous 1. What is setup synta...

Detailed explanation of Nginx configuration required for front-end

Nginx (engine x) is a lightweight, high-performan...

JavaScript canvas to load pictures

This article shares the specific code of JavaScri...

Detailed installation tutorial of mysql 5.7.11 under Win7 system

Operating system: Win7 64-bit Ultimate Edition My...

Implement a simple search engine based on MySQL

Table of contents Implementing a search engine ba...

Solve the problem of black screen when starting VMware virtual machine

# Adjust VMware hard disk boot priority Step 1: E...

Tutorial on installing and configuring remote login to MySQL under Ubuntu

This article shares the MySQL installation and co...

Vue component organization structure and component registration details

Table of contents 1. Component Organization 2. Co...