Table of Contents
MySQL常用内置函数 
Home Database Mysql Tutorial MySQL学习笔记_7_MySQL常用内置函数_MySQL

MySQL学习笔记_7_MySQL常用内置函数_MySQL

Jun 01, 2016 pm 01:19 PM
where programming

bitsCN.com

MySQL常用内置函数 

说明:

1)可以用在SELECT/UPDATE/DELETE中,及where,orderby,having中

2)在函数里将字段名作为参数,变量的值就是字段所对应的每一行的值。

3)在程序设计语言如C++中提供的函数,MySQL大部分也提供了,关于MySQL函数的完整信息,请参阅《MySQL参考手册》

一、字符串函数【比较常用,需要掌握】

1、 concat(s1,s2,...,sn) #把传入的参数连接成一个字符串

selectconcat('abc','def');

selectconcat(name,' age is ',age) from users; 

2、insert(str,m,n,inser_str) #将str的从m位置开始的n个字符替换为inser_str

selectinsert('abcdef',2,3,'123456');

selectinsert(name,3,2,'HAHA') from users;

selectinsert(name,2,2,'00') from users;

3、lower(str)/upper(str) #将字符串str转换成小写/大写

selectlower('HELLO'),upper('hello');

selectlower('HELLO') as 'HELLO',upper('hello')as 'HELLO';

select* from users where upper(name) = 'AAA';

4、left(str,n)/right(str,n) #分别返回str最左边/最右边的n个字符,如果n NULL 则任何东西不返回

selectleft('123',3),right('123456',3),left('123',NULL);

5、lpad(str,n,pad)/rpad(str,n,pad) #用字符串pad对str的最左边/最右边进行填充,知道满足str含有n个字符为止

selectname,lpad(name,10,'#'),rpad(name,10,'@') from users;

6、trim(str)/ltrim(str)/rtrim(str) #去除字符串str左右空格/左空格/右空格

selectconcat('#',trim(" abc "),'#'),concat('#',ltrim(' abc '),'#'),concat('#',rtrim(' abc '),'#');

7、replace(str,sear_str,sub_str) #将字符串str中所有出现的sear_str字符串替换为sub_str

select replace('abcdefgabcd','cd','XXX') ;

8、strcmp(str1,str2) #以ASCII码比较字符串str1,str2,返回-1(str1 str2)

selectstrcmp('aa','bb'),strcmp('aa','aa'),strcmp('bb','aa');

9、substring(str,n,m) #返回字符串str中从n起,m个字符长度的字符串

selectsubstring('abcdef',2,3);

selectname,substring(name,1,2) as subname from users;

二、数值函数

1、abs(x) #返回x的绝对值

selectabs(10),abs(-10);

selectabs(age) from users;

2、ceil(x) #返回大于x的最小整数

3、floor(x) #返回小于x的最大整数

selectceil(2.1),ceil(2.5),ceil(2.9),floor(2.1),floor(2.5),floor(2.9);

4、mod(x,y) #返回x/y的模,与x%y作用相同

selectmod(null,11);

5、rand() #返回0~1之间的随机数

selectrand();

selectceil(rand() * 100); #取0~100之间的整数随机数

selectfloor(rand() * 100);

6、round(n,m) #返回n四舍五入之后含有m位小数的值,m值默认为0

selectround(1.23);

selectround(1.456,2);

7、truncate(n,m) #返回数字n被截断为m位小数的数值

selecttruncate(1.234,2);

selecttruncate(1.235,2),round(1.235,2);

三、日期函数

1、curdate() #返回当前日期

2、curtime() #返回当前时间

selectcurdate(),curtime();

3、now() #返回当前日期+时间

selectnow();

4、unix_timestamp(now())#返回unix当前时间的时间戳

selectunix_timestamp(now()); #从计算机元年(1971-1-100:00:00)到现在的秒数

5、from_unixtime() #将时间戳(整数)转换为“日期+时间(xx-xx-xxxx:xx:xx)”的形式

selectfrom_unixtime(1392853616);

6、week(now()) #返回当前时间是第几周

7、year(now()) #返回当前是XX年

8、hour(now())/hour(curtime()) #返回当前时间的小时数

9、minute(curtime()) #返回当期的分钟数

...

selectweek(now()),year(now()),hour(now());

selectweek(from_unixtime(1392853616)); #返回unix时间戳中的周期数

10、monthname(now())/monthname(curdate()) #返回当前月的英文名

11、date_format(now(),"%Y-%M-%D%H:%I%S") #将当期时间格式化

selectdate_format(now(),"%Y-%m-%d %H:%i%s");

selectdate_format(now(),"%y%m%d %H:%i%s");

四、流程控制函数

1、if(value,true,false) #如果value值为真,则返回true,否则,返回false

selectif (salary > 3000,'Hight','Low') from salary;

selectid,salary, if (salary NULL,'NULL','NOT NULL') from salary;

2、ifnull(value1,value2)#如果value1不为空,则返回value1,不然返回value2

#可以用来进行空值替换

selectifnull(salary,0.00) from salary;

3、casewhen [value] then … else …end #如果value值为真,执行then之后的语句,不然执行eles后的语句,不要忘记end!

selectcase when salary

五、其他函数

1、database() #当前数据库

2、version() #当前数据库版本

3、user() #当前登录用户

selectdatabase();

4、inet_aton(ip) #ip地址的网络字节顺序

selectinet_aton('192.168.139.1');

5、inet_ntoa #返回数字所代表的ip

selectinet_ntoa(3232271105);

6、password(str) #返回加密的str字符串

selectpassword("123456"); #返回一个41位长的加密字符串,只是用于给MySQL系统用户进行加密

7、md5() #在应用程序中进行数据加密,比如在C++程序中

selectmd5(“123456”);

bitsCN.com
Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A Practical Guide to the Where Method in Laravel Collections A Practical Guide to the Where Method in Laravel Collections Mar 10, 2024 pm 04:36 PM

Practical Guide to Where Method in Laravel Collections During the development of the Laravel framework, collections are a very useful data structure that provide rich methods to manipulate data. Among them, the Where method is a commonly used filtering method that can filter elements in a collection based on specified conditions. This article will introduce the use of the Where method in Laravel collections and demonstrate its usage through specific code examples. 1. Basic usage of Where method

How to use the Where method in Laravel collections How to use the Where method in Laravel collections Mar 10, 2024 pm 10:21 PM

How to use the Where method in Laravel collection Laravel is a popular PHP framework that provides a wealth of functions and tools to facilitate developers to quickly build applications. Among them, Collection is a very practical and powerful data structure in Laravel. Developers can use collections to perform various operations on data, such as filtering, mapping, sorting, etc. In collections, the Where method is a commonly used method for filtering the collection based on specified conditions.

What is the instruction design and debugging process called design? What is the instruction design and debugging process called design? Jan 20, 2021 pm 03:44 PM

The process of instruction design and debugging is called "programming". A sequence of instructions designed to solve a specific problem is called a program, and programming is the process of giving a program to solve a specific problem and is an important part of software construction activities. The programming process should include different stages such as analyzing problems, designing algorithms, writing programs, testing, and debugging.

A required course to become a top front-end engineer! A required course to become a top front-end engineer! Mar 25, 2024 pm 04:30 PM

A required course to become a top front-end engineer! With the rapid development and popularity of the Internet, the front-end development industry has become more and more popular. As the link between users and products, front-end engineers play a vital role in the technical field. They not only need to have solid technical skills, but also need to continuously learn and improve themselves to maintain industry competitiveness. To become a top front-end engineer, in addition to possessing basic skills, you also need to master a series of required courses. 1. Master the basics of HTML, CSS and JavaScript

From beginner to proficient: Master the skills of using is and where selectors From beginner to proficient: Master the skills of using is and where selectors Sep 08, 2023 am 09:15 AM

From beginner to proficient: Master the skills of using is and where selectors Introduction: In the process of data processing and analysis, the selector is a very important tool. Through selectors, we can extract the required data from the data set according to specific conditions. This article will introduce the usage skills of is and where selectors to help readers quickly master the powerful functions of these two selectors. 1. Use of the is selector The is selector is a basic selector that allows us to select the data set based on given conditions.

What software is used for C language programming? What software is used for C language programming? Jan 27, 2024 pm 02:36 PM

C language programming software: 1. Visual Studio Code; 2. Code::Blocks; 3. Dev-C++; 4. Eclipse CDT; 5. CLion; 6. GCC; 7. Xcode. Detailed introduction: 1. Visual Studio Code, which is a free open source code editor developed by Microsoft, supports multiple programming languages, including C language. VS Code can be easily configured to suit C language development by installing various plug-ins. wait.

Recommended essential software for C language programming Recommended essential software for C language programming Feb 19, 2024 pm 12:58 PM

In the field of computer science, C language is a widely used programming language with the characteristics of high efficiency and flexibility. Therefore, learning and mastering C language programming has become a required course for many computer majors and programming enthusiasts. However, to learn and use C language effectively, some necessary software tools are indispensable. This article will introduce several recommended essential software for C language programming. First, let’s recommend a powerful integrated development environment (IDE)—Code::Blocks. Code::Bloc

Analysis of 10 PHP frameworks and their advantages Analysis of 10 PHP frameworks and their advantages May 26, 2023 am 08:10 AM

With the continuous development of PHP technology, more and more PHP frameworks have emerged. PHP framework can help developers build web applications more quickly and efficiently. But different frameworks are suitable for different projects, so it's important to understand the advantages and disadvantages of each framework. In this article, we will introduce 10 PHP frameworks and their main advantages. Laravel Laravel is one of the most popular PHP frameworks that provides a simple and elegant way to build web applications. Laravel has a huge

See all articles