当前位置:网站首页>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 .
边栏推荐
- Connect function usage of socket
- B tree code (C language)
- 请你描述下从浏览器上输入一个url到呈现出页面的整个过程。
- 智能计算之神经网络(BP)介绍
- [petty bourgeoisie database] break down the concept: data, database, database system, database management system, database technology
- Random talk about redis source code 122
- [deeply understand tcapulusdb technology] create a game zone
- 一文搞懂 MySQL 中 like 的索引情况
- 【深入理解TcaplusDB技术】创建游戏区
- 基于Sentinel的高可用限流系统的Grafana报表展示
猜你喜欢

How should programmers look up dates

芯和半导体“射频EDA/滤波器设计平台”闪耀IMS2022

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

不断重修的计划与变化
![[in depth understanding of tcapulusdb technology] introduction tcapulusdb problem summary](/img/2b/3ab5e247ac103728b4d3579c3c5468.png)
[in depth understanding of tcapulusdb technology] introduction tcapulusdb problem summary

#夏日挑战赛# 【FFH】从零开始的鸿蒙机器学习之旅-NLP情感分析

Possible security vulnerabilities in NFT

科技云报道:东数西算不止于“算”,更需“新存储”

An IPFs enabled email - skiff

What can the accelerated implementation of digital economy bring to SMEs?
随机推荐
【Proteus仿真】三极管组成的H桥驱动直流电机+按键正反转控制
漫话Redis源码之一百一二十
Redis中的Multi事务
智能计算之神经网络(BP)介绍
漫话Redis源码之一百二十二
[deeply understand tcapulusdb technology] view the online operation of tcapulusdb
#夏日挑战赛# 【FFH】从零开始的鸿蒙机器学习之旅-NLP情感分析
金鱼哥RHCA回忆录:DO447管理用户和团队的访问--创建和管理Ansible Tower用户
芯和半导体“射频EDA/滤波器设计平台”闪耀IMS2022
Three dimensional world helps the laboratory to consolidate the complete quality system management
元宇宙中的云计算,提升你的数字体验
[in depth understanding of tcaplus DB technology] Introduction to tcaplus JDBC development
University of Calgary | recommendation system based on Reinforcement Learning
防火墙基础之安全策略和NAT(Easy IP)
[in depth understanding of tcapulusdb technology] tcapulusdb regular documents
Fibonacci search (golden section)
Implementation of balanced binary tree with C language
[deeply understand tcapulusdb technology] tcapulusdb table management - create a new table
漫话Redis源码之一百一十九
Cloud computing in the metauniverse to enhance your digital experience