(9):Mysql常用函数小结_MySQL
bitsCN.com
一、String Functions
String functions is one of the most commonly used for Mysql.
(1):concat(s1,s2,..Sn)
连接s1,s2,...sn为一个字符串。如:
<code>SELECT CONCAT('|',2,'*','&'); #|2*&SELECT concat(50,'%'); #50%SELECT concat('abc',null) #Null</code>
(2):insert(str,x,y,instr)
将字符串str从第x位置开始,y个字符长的子串替换为字符串instr
<code>SELECT INSERT('I love Jack',8,4,'Lada'); #start 1</code>
(3):lower(str)
、upper(str)
大小写转换
(4):left(str,x)
、right(str,x)
分别返回字符串最左边的x个字符和最右边的x个字符,如果第二个参数的null
则不返回任何字符串。
<code>SELECT left('My name is',2); #MySELECT right('My name is',2); #isSELECT left('Jack',NULL); #Null</code>
(5):lpad(str,n,pad)
、rpad(str,n,pad)
用于字符串pad对str最左边和最右边进行填充,直到长度为n个字符长度。
<code>SELECT lpad('hi',5,'?') #???hiSELECT rpad('hi',10,'Jack') #hiJackJack</code>
(6):ltrim(str)
、rtrim(str)
去掉字符串str左侧和右侧的空格。
<code>SELECT ltrim(' hi'); #hiSELECT rtrim('hi '); #hi</code>
(7):repeat(str,x)
返回str重复x次的结果
<code>SELECT repeat('*',10) #**********</code>
(8):replace(str,a,b)
用字符串b替换str的所有出现的字符串a
<code>SELECT replace('L love Jan','J','B'); #L love Ban</code>
(9):strcmp(str1,str2)
比较字符串str1和str2的ASCII码值大小
<code>SELECT strcmp('A','a'),strcmp('a','a') # 0 0</code>
(10):trim(str)
去掉字符串str首尾空格
(11):substring(str,x,y)
返回从字符串str中第x位置其y个字符长度的子串。
<code>SELECT substring('good',2,4) #ood,位置[x,y]</code>
二、数值函数
(1):abs(x)
:返回x的绝对值
(2):ceil(x)
:返回大于x的最小整数值
<code>SELECT ceil(-0.8),ceil(0.8) #0 1</code>
(3):floor(x)
返回小于x的最大整数值
<code>SELECT floor(-0.8),floor(0.8) #-1 0</code>
(4):mod(x,y)
返回x/y的模
<code>SELECT mod(5,3) #2</code>
(5):rand()
:返回0~1之间的随机数
SELECT rand(),round(10*rand(),2)
#0.87565597228301
#1.47
(6):round(x,y)
返回参数x的四舍五入的有y位小数的值
(7):truncate(x,y)
返回数字x截断y位小数的结果
<code>SELECT truncate(rand(),3) #0.109</code>
三、日期和时间函数
(1):curdate()
、curtime()
、now()
:返回当前日期、当前时间、当前日期时间
<code>SELECT curdate() #2013-10-23SELECT curtime() #13:37:04SELECT now() #2013-10-23 13:37:18</code>
(2):week(date)
:返回日期date为一年中的第几周
<code>SELECT week(now()) #42</code>
(3):year(date)
、hour(time)
、minute(time)
、second(time)
返回当前年份、小时、分钟、秒
<code>SELECT year(now()) #2013SELECT hour(now()) #13SELECT minute(now()) #53SELECT second(now()) #24</code>
(4):monthname(date)
返回月份名
<code>SELECT monthname(now()) #October</code>
(5):date_format(date,fmt)
:按字符串fmt格式化日期date值,此函数能够按指定日期显示
<code>SELECT date_format(now(),'%y-%m-%d %H:%I:%s') </code>
%S, %s 两位数字形式的秒( 00,01, ..., 59)
%I, %i 两位数字形式的分( 00,01, ..., 59)
%H 两位数字形式的小时,24 小时(00,01, ..., 23)
%h 两位数字形式的小时,12 小时(01,02, ..., 12)
%k 数字形式的小时,24 小时(0,1, ..., 23)
%l 数字形式的小时,12 小时(1, 2, ..., 12)
%T 24 小时的时间形式(hh:mm:ss)
%r 12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
%p AM或PM
%W 一周中每一天的名称(Sunday, Monday, ..., Saturday)
%a 一周中每一天名称的缩写(Sun, Mon, ..., Sat)
%d 两位数字表示月中的天数(00, 01,..., 31)
%e 数字形式表示月中的天数(1, 2, ..., 31)
%D 英文后缀表示月中的天数(1st, 2nd, 3rd,...)
%w 以数字形式表示周中的天数( 0 = Sunday, 1=Monday, ..., 6=Saturday)
%j 以三位数字表示年中的天数( 001, 002, ..., 366)
%U 周(0, 1, 52),其中Sunday 为周中的第一天
%u 周(0, 1, 52),其中Monday 为周中的第一天
%M 月名(January, February, ..., December)
%b 缩写的月名( January, February,...., December)
%m 两位数字表示的月份(01, 02, ..., 12)
%c 数字表示的月份(1, 2, ...., 12)
%Y 四位数字表示的年份
%y 两位数字表示的年份
%% 直接值“%”
(6):datediff(date1,date2)
计算两个日期之间相差的天数
<code>SELECT datediff('2015-8-8',now()) #654</code>
四、流程控制
(1):if(value,t,f)
如果value为真,返回t,否则返回f
<code>mysql> SELECT if(salary>1500,'高','低') FROM salary;+---------------------------+| if(salary>1500,'高','低') |+---------------------------+| 低|| 低|| 高|| 高|+---------------------------+4 rows in set</code>
(2):ifnull(value1,value2)
如果value1不为空返回value1,否则返回value2。这个函数一般用来替换Null的值。
<code>mysql> select ifnull(salary,0) from salary;+------------------+| ifnull(salary,0) |+------------------+| 1000 || 1500 || 2000 || 2500 ||0 |+------------------+5 rows in set</code>
(3):case when [value1] then [result1] ..else[default] end
如果value1为真,返回result1,否则返回default
<code>mysql> select case when salary>1500 then '高' else '低' end from salary;+-----------------------------------------------+| case when salary>1500 then '高' else '低' end |+-----------------------------------------------+| 低|| 低|| 高|| 高|| 低|+-----------------------------------------------+5 rows in set</code>
(4):case [expr] when [value1] then [result1] ..else[default] end
如果expr等于value1,返回result1,否则返回default
<code>mysql> select case salary when 1000 then '低' when 2000 then '中' else '高' end from salary;+-------------------------------------------------------------------+| case salary when 1000 then '低' when 2000 then '中' else '高' end |+-------------------------------------------------------------------+| 低|| 高|| 中|| 高|| 高|+-------------------------------------------------------------------+5 rows in set</code>
小技巧:
<code>mysql> select case when instr(type,'上季') then 1 when instr(type,'下季') then 2 else 3 end from salary;+-------------------------------------------------------------------------------+| case when instr(type,'上季') then 1 when instr(type,'下季') then 2 else 3 end |+-------------------------------------------------------------------------------+| 1 || 1 || 2 || 3 || 1 |+-------------------------------------------------------------------------------+5 rows in set</code>
五、其他函数
(1):database()
:返回当前数据库名
(2):version()
:返回当前数据库版本
(3):user()
:返回当前登录用户名
其他参考文档。
参考:《深入浅出mysql》

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

As a scripting language widely used to develop web applications, PHP has very powerful string processing functions. In daily development, we often encounter operations that require deleting a string, especially the last two characters of the string. This article will introduce two PHP techniques for deleting the last two characters of a string and provide specific code examples. Tip 1: Use the substr function The substr function in PHP is used to return a part of a string. We can easily remove characters by specifying the string and starting position
