search
HomeDatabaseMysql TutorialWhat are the advanced query functions of MySQL?
What are the advanced query functions of MySQL?Oct 13, 2020 pm 03:34 PM
mysqlquery function

MySQL advanced query functions: 1. String function; 2. Numeric function, [CEIL(x)] returns the smallest integer value not less than X; 3. Date function, [DATE_ADD/DATE_SUB], etc.

What are the advanced query functions of MySQL?

MySQL advanced query functions:

Classification of functions:

1, Single-line function: Calculate the input value of each record, obtain the corresponding calculation result, and return it to the user. In other words, each record is used as an input parameter, and the calculation result of each record is obtained through function calculation.

2, Multi-line function: Calculate the input values ​​​​of multiple records and obtain a single result corresponding to multiple records.

Single-line function:

①:String function (users process single-line character data, such as case conversion, string interception, assembly, etc.)

a.LOWER/UPPER(LOWER(str): Returns the string str in which the string str becomes lowercase letters. UPPER(str): Returns the string str in which the string str becomes uppercase letters. )  

SELECT UPPER(name) FROM student; //全部大写
SELECT LOWER(name) FROM student; //全部小写

b.CONCAT: CONCAT(str1,str2,...):

 1, the return result is the string generated by the connection parameters.

2. If any parameter is NULL, the return value is NULL

3. One or more parameters are allowed

SELECT name,age, CONCAT(name,'-',age) FROM student;

c.INSERT: Replace the specified (position, length) substring with the target string

Format: INSERT(str,pos,len,newstr)

Parameters: str: (source character String) pos: (the starting position of insertion, the index starts from 1) len: (the length of the replacement string) newstr: (the string to be inserted)

1, return the string str, its substring Starting at position pos and length replaced by len characters of the string newstr.

    2. If pos exceeds the string length, the return value is the original string.

     3. If the length of len is greater than the length of other strings, replacement starts from position pos.

      4. If any parameter is null, the return value is NULL

Example:

Replace some characters of the user name, the rules are as follows: keep the first 2 digits of the user name , use * instead of the middle 3 digits. If there are extra characters in the name, keep

SELECT  INSERT(name,2,3,'***')  FROM student;

d. ①LENGTH: The number of bytes occupied by the string

SELECT LENGTH(name) FROM student;

②CHAR_LENGTH: Calculate the number of characters

SELECT CHAR_LENGTH(name) FROM student;

e: LPAD/RPAD: If the number of characters in the string is greater than the given number, if it is less, it will be filled in from the edge specified by the function Specify the number, if there are more, truncate

from the end of the string. LPAD(str,len,padstr): left padding

1, return the string str, whose left side is represented by The string padstr is padded to a total length of len.

  2. If the length of str is greater than len, the return value is shortened to len characters.

SELECT LPAD(NAME,10,'*')  FROM student;

Result display:

RPAD(str,len,padstr): right padding

1, returns the string str, the right side of which is padded to len characters by the string padstr length.

  2. If the length of the string str is greater than len, the return value is shortened to the same length as len characters.

SELECT RPAD(NAME,10,'*')  FROM student;

f:TRIM/LTRIM/RTRIM

LTRIM (str): The left space is trimmed;

RTRIM (str): The right space is trimmed;

TRIM(str)=LTRIM RTRIM

TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)

Advanced usage, intercept from str in the specified way remstr;

TRIM(remstr FROM] str): equivalent to TRIM(BOTH remstr FROM str);

SELECT TRIM(name), CHAR_LENGTH(TRIM(name)), CHAR_LENGTH(name) FROM student;
#去掉字符串中两端的指定子字符串
SELECT TRIM('ja' FROM name) FROM student;
                            ||(等价于)
SELECT TRIM(BOTH 'ja' FROM name) FROM student;        
# 去掉头
SELECT TRIM(LEADING 'ja' FROM name) FROM student;
# 去掉尾
SELECT TRIM(TRAILING 'ja' FROM name) FROM student;

g:REPLACE

REPLACE( str, from_str, to_str):

1. Replace all from_str with to_str in str;

2. Case sensitive;

# 选择性的替换
# 当某一条的记录中的字段值和第二个参数的值相等的时候
#把这个字段值替换成字三个参数
SELECT REPLACE(name,'rose','niceMan') FROM student;

h:SUBSTRING (str,pos):

Returns a substring from the string str, starting at position pos

SUBSTRING(str,pos,len):

Return a substring with the same length as len characters from the string str, starting at position pos

If pos is a negative number, counting from the end of the string;

# 从指定的位置开始,截取到最后
SELECT SUBSTR(name,2) FROM student;
# 从指定的位置截取指定的长度的子字符串
SELECT SUBSTR(name,2,3) FROM student;

②: Numeric function

a.ABS/MOD ABS(x): Returns the absolute value of a number;

MOD(N,M): Returns N divided by M Remainder (modulo);

SELECT ABS(-13);   //取绝对值
SELECT MOD(10,3);//取模

b.CELT/FLOOR/ROUND/TRUNCATE

CEIL(x): Returns the smallest integer value that is not less than X;

SELECT CEIL(3.5);      结果4

FLOOR (x): Returns the largest integer value that is not greater than ,D):

 1. Return parameter X, whose value is close to the nearest integer.

  2,在有两个参数的情况下,返回X ,其值保留到小数点后D位,而第D位的保留方式为四舍五入。

  3,若要接保留X值小数点左边的D 位,可将 D 设为负值。

 

SELECT ROUND(3.2228,2);    返回3.22

  TRUNCATE(X,D)

  1,返回被舍去至小数点后D位的数字X。

  2,若D 的值为 0, 则结果不带有小数点或不带有小数部分。可以将D设为负数,若要截去(归零) X小数点左起第D位开始后面所有

SELECT TRUNCATE(3.456,1)    返回3.4

③:日期函数

a:DATE_ADD/DATE_SUB

  TYPE:SECOND ,MINUTE ,HOUR ,DAY ,WEEK ,MONTH ,YEAR

  1,执行日期运算;

  2,date 是一个 DATETIME 或DATE值,用来指定起始时间;

  3,expr 是一个字符串表达式,用来指定从起始日期添加或减去的时间间隔值;

  4,type 为关键词,它指示了表达式被解释的方式

  DATE_ADD(date,INTERVAL expr type)

  DATE_SUB(date,INTERVAL expr type)

SELECT DATE_ADD(CURDATE(),INTERVAL 1 DAY)

SELECT DATE_SUB(CURDATE(),INTERVAL 1 DAY)

b:DATEDIFF(expr,expr2):返回起始时间expr和结束时间expr2之间的天数

#计算两个日期的差值, 计算结果的单位是·天·

SELECT DATEDIFF('2017-03-21','2017-03-10')

c:DateTime_module (YEAR,DAY,LAST_DAY,MONTH,HOUR,MINUTE)

# 获取某个日期的模块的值, 年,月日时分秒
SELECT DAY(now())
SELECT DAYOFMONTH(now())
SELECT DAYOFWEEK(now())
SELECT DAYOFYEAR(now())
SELECT now()
SELECT HOUR(now())
SELECT MINUTE(now())

e:UNIX_TIMESTAMP/FROM_UNIXTIME

  UNIX_TIMESTAMP(date):将返回从'1970-01-01 00:00:00' GMT 指定日期的后的秒数
  FROM_UNIXTIME(unix_timestamp) FROM_UNIXTIME(unix_timestamp,format)
SELECT UNIX_TIMESTAMP(NOW())
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()))
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()), '%y/%m/%d%H:%i:%S')

④:其他函数

a.UUID

SELECT UUID();

b:COALESCE

  COALESCE(value,...):返回值为列表当中的第一个非 NULL值,在没有非NULL 值得情况下返回值为 NULL
SELECT COALESCE('Jerry', 'Jack', 'Lucy');  结果为Jerry
SELECT COALESCE(NULL, 'Jack', 'Lucy');   结果为Jack

e:IF/IFNULL语句

 

# 数据库中的if函数, 相当于Java中的三目运算符
SELECT IF(1>1,'true','false')
# IFNULL(expr1,expr2):
#假如expr1 不为 NULL,则 IFNULL() 的返回值为expr1; 否则其返回值为expr2。    
SELECT IFNULL(NULL,10);
SELECT IFNULL(NULL,'unempty')

更多相关免费学习推荐:mysql教程(视频)

The above is the detailed content of What are the advanced query functions of MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment