当前位置:网站首页>MySQL Basics - functions
MySQL Basics - functions
2022-06-22 20:23:00 【White chocolate x】
Catalog
In the last article, we learned MySQL Basics ——DCL sentence , This article is for learning MySQL Basics —— function .
function
A function is a program or code that can be called directly by another program .
The following code demonstrates the data table structure and data as follows :

String function
stay MySQL Common string functions are shown in the following table :
| function | function |
|---|---|
| CONCAT(S1,S2,...Sn) | String splicing , take S1,S2,...Sn Concatenate into a string |
| LOWER(str) | The string str All to lowercase |
| UPPER(str) | The string str Capitalize all |
| LPAD(str,n,pad) | padding-left , Use string pad Yes str Fill the left side of the , achieve n String length |
| RPAD(str,n,pad) | Right fill , Use string pad Yes str Fill the right side of the , achieve n String length |
| TRIM(str) | Remove the spaces at the beginning and end of the string |
| SUBSTRING(str,start,len) | Return from string str from start It's from the position len A string of length |
The syntax format of using string function is as follows :
function ( Parameters );The sample code is as follows :
SELECT CONCAT('hello','world'); # String concatenation As shown in the figure below :

When we need to use string functions to operate on the data table , The sample code is as follows :
UPDATE students SET uid =RPAD(uid,5,'0'); # Fill right uid
SELECT * FROM students; # View the data As shown in the figure below :

Numerical function
stay MySQL The commonly used numerical functions are shown in the following table :
| function | function |
|---|---|
| CEIL(x) | Rounding up |
| FLOOR(x) | Rounding down |
| MOD(x,y) | return x/y The mold |
| RAND() | return 0~1 The random number in |
| ROUND(x,y) | Find parameters x Rounded value of , Retain y Decimal place |
The syntax format for using numeric functions is as follows :
function ( Parameters );The sample code is as follows :
SELECT RAND(); # Random generation 0~1 The random number in As shown in the figure below :

Date function
stay MySQL Common date functions are shown in the following table :
| function | function |
|---|---|
| CURDATE() | Return current date |
| CURTIME() | Return current time |
| NOW() | Returns the current date and time |
| YEAR(date) | Get specified date A year of |
| MONTH(date) | Get specified date Month of |
| DAY(date) | Get specified date Date |
| DATE_ADD(date,INTERVAL expr type) | Return a date / Time value plus a time interval expr Time value after |
| DATEDIFF(date1,date2) | Return to start time date1 And end time date2 Days between |
The syntax format of the date function is as follows :
function ( Parameters );The sample code is as follows :
SELECT CURDATE(); # Get the current time
SELECT YEAR(CURDATE()); # Get the year of the current time
SELECT DATE_ADD(CURDATE(),INTERVAL 70 DAY); # Gets the current time plus 70 God As shown in the figure below :

Flow function
stay MySQL Common process functions are shown in the following table :
| function | function |
|---|---|
| IF(value,t,f) | If value by true, Then return to t, Otherwise return to f |
| IFNULL(value1,value2) | If value1 Not empty , return value1, Otherwise return to value2 |
| CASE WHEN [val1] THEN [res1] ... ELSE [default] END | If val1 by true, return res1,... Otherwise return to default The default value is |
| CASE [expr] WHEN [val1] THEN [res1] ... ELSE [default] END | If expr The value is equal to the val1, return res1,... Otherwise return to default The default value is |
The syntax format of the process function is as follows :
function ( Parameters );The sample code is as follows :
SELECT IF(true,'OK','Error');
SELECT IFNULL(NULL,'OK');
SELECT uname ,(CASE usex WHEN ' male ' THEN '1' WHEN ' Woman ' THEN '2' ELSE '0' END) AS ' Gender ' FROM students;As shown in the figure below :

Okay ,MySQL Basics —— That's all for the function , In the next article, we will learn MySQL Basics —— constraint .
边栏推荐
- 3个月自学自动化测试,薪资从4.5K到15K,鬼知道我经历了什么?
- Web technology sharing | [Gaode map] to realize customized track playback
- Merge sort (recursive and iterative Implementation)
- A text to show you the memory leak
- Mysql database knowledge points (III)
- MySQL基础——约束
- Google | ICML 2022: sparse training status in deep reinforcement learning
- Random talk about redis source code 122
- Search, insert and delete of binary sort tree
- 【深入理解TcaplusDB技术】单据受理之创建游戏区
猜你喜欢
![Web technology sharing | [Gaode map] to realize customized track playback](/img/80/7daba6716b85276de8d09b9d016313.png)
Web technology sharing | [Gaode map] to realize customized track playback

Using span method to realize row merging of multi-layer table data

播放增长900w,B站用户叫好叫座的恰饭总结

3个月自学自动化测试,薪资从4.5K到15K,鬼知道我经历了什么?

不断重修的计划与变化

科技云报道:东数西算不止于“算”,更需“新存储”
![[in depth understanding of tcaplus DB technology] getting started tcaplus SQL driver](/img/2b/3ab5e247ac103728b4d3579c3c5468.png)
[in depth understanding of tcaplus DB technology] getting started tcaplus SQL driver
![[deeply understand tcapulusdb technology] view the online operation of tcapulusdb](/img/6f/2d62030e631e3085acf72951f2416f.png)
[deeply understand tcapulusdb technology] view the online operation of tcapulusdb

一个支持IPFS的电子邮件——SKIFF

芯和半导体“射频EDA/滤波器设计平台”闪耀IMS2022
随机推荐
[in depth understanding of tcaplus DB technology] Introduction to tcaplus JDBC development
数字化转型的失败原因及成功之道
智能计算之神经网络(Hopfield网络-DHNN,CHNN )介绍
程序员应该怎么查日期
不断重修的计划与变化
ROS从入门到精通(八) 常用传感器与消息数据
undefined reference 之坑
3个月自学自动化测试,薪资从4.5K到15K,鬼知道我经历了什么?
【Proteus仿真】三极管组成的H桥驱动直流电机+按键正反转控制
Zabbix学习笔记(三十七)
年中大促 | 集成无忧,超值套餐 6 折起
Peking University - robust task representation for off-line meta reinforcement learning through contrastive learning
[deeply understand tcapulusdb technology] cluster management operation
科技云报道:东数西算不止于“算”,更需“新存储”
University of Calgary | recommendation system based on Reinforcement Learning
手把手教你IDEA创建SSM项目结构
R语言data.table导入数据实战:data.table数据列名称的重命名(rename)
Containerd容器运行时(2):yum安装与二进制安装,哪个更适合你?
【深入理解TcaplusDB技术】入门Tcaplus SQL Driver
[deeply understand tcapulusdb technology] tcapulusdb table management - create a new table