Home Database Mysql Tutorial MySQL惯用函数 二

MySQL惯用函数 二

Jun 07, 2016 pm 04:23 PM
mysql function Commonly used combine Bring your own

MySQL常用函数 二 结合MySQL自带的帮助文档列一下MySQL数据库中常用的一些函数。 事实证明:MySQL的联机帮助资料非常实用,希望哪一天可爱的Oracle可以像MySQL学习一下,她可以让您基本不用查看其他的资料就将函数的基本使用方法和示例尽收眼底。 废话少说,

MySQL常用函数 二

结合MySQL自带的帮助文档列一下MySQL数据库中常用的一些函数。
事实证明:MySQL的联机帮助资料非常实用,希望哪一天可爱的Oracle可以像MySQL学习一下,她可以让您基本不用查看其他的资料就将函数的基本使用方法和示例尽收眼底。
废话少说,直入主题

一、常用字符串函数

二、数值函数

三、日期和时间函数

四、MySQL控制流程函数

1.IF(expr1,expr2,expr3)函数:如果expr1为真则返回expr2,否则返回expr3
2.IFNULL(expr1,expr2)函数:若expr1为NULL则返回expr2内容
3.NULLIF(expr1,expr2)函数:若expr1 = expr2则返回NULL,否则返回expr1

4.CASE函数

?

五、其他常用函数

时间格式:或者将里面的和java对比的将答谢的换程小写。

?根据format字符串格式化date值。下列修饰符可以被用在format字符串中: %M 月名字(January……December)?
%W 星期名字(Sunday……Saturday)?
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)?
%Y 年, 数字, 4 位?
%y 年, 数字, 2 位?
%a 缩写的星期名字(Sun……Sat)?
%d 月份中的天数, 数字(00……31)?
%e 月份中的天数, 数字(0……31)?
%m 月, 数字(01……12)?
%c 月, 数字(1……12)?
%b 缩写的月份名字(Jan……Dec)?
%j 一年中的天数(001……366)?
%H 小时(00……23)?
%k 小时(0……23)?
%h 小时(01……12)?
%I 小时(01……12)?
%l 小时(1……12)?
%i 分钟, 数字(00……59)?
%r 时间,12 小时(hh:mm:ss [AP]M)?
%T 时间,24 小时(hh:mm:ss)?
%S 秒(00……59)?
%s 秒(00……59)?
%p AM或PM?
%w 一个星期中的天数(0=Sunday ……6=Saturday )?
%U 星期(0……52), 这里星期天是星期的第一天?
%u 星期(0……52), 这里星期一是星期的第一天?
%% 一个文字“%”。

所有的其他字符不做解释被复制到结果中。

mysql> select DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y');?
-> 'Saturday October 1997'?
mysql> select DATE_FORMAT('1997-10-04 22:23:00', '%H:%i:%s');?
-> '22:23:00'?
mysql> select DATE_FORMAT('1997-10-04 22:23:00',?
'%D %y %a %d %m %b %j');?
-> '4th 97 Sat 04 10 Oct 277'?
mysql> select DATE_FORMAT('1997-10-04 22:23:00',?
'%H %k %I %r %T %S %w');?
-> '22 22 10 10:23:00 PM 22:23:00 00 6'



一、常用字符串函数
1.CONCAT(str1,str2,...)

mysql> ? concat;
mysql> SELECT CONCAT('My', 'S', 'QL');
+-------------------------+
| CONCAT('My', 'S', 'QL') |
+-------------------------+
| MySQL??????????????????? |
+-------------------------+

2.INSERT(str,pos,len,newstr)
mysql> ? insert function;
mysql> SELECT INSERT('Quadratic', 3, 4, 'What');
+-----------------------------------+
| INSERT('Quadratic', 3, 4, 'What') |
+-----------------------------------+
| QuWhattic????????????????????????? |
+-----------------------------------+

3.LOWER(str)
mysql> ? lower
mysql> SELECT LOWER('QUADRATICALLY');
+------------------------+
| LOWER('QUADRATICALLY') |
+------------------------+
| quadratically?????????? |
+------------------------+

4.UPPER(str)
mysql> ? upper
mysql> SELECT UPPER('Hej');
+--------------+
| UPPER('Hej') |
+--------------+
| HEJ?????????? |
+--------------+

5.LEFT(str,len)
mysql> ? left
mysql> SELECT LEFT('foobarbar', 5);
+----------------------+
| LEFT('foobarbar', 5) |
+----------------------+
| fooba???????????????? |
+----------------------+

6.RIGHT(str,len)
mysql> ? right
mysql> SELECT RIGHT('foobarbar', 4);
+-----------------------+
| RIGHT('foobarbar', 4) |
+-----------------------+
| rbar?????????????????? |
+-----------------------+

7.LPAD(str,len,padstr)
mysql> ? lpad
mysql> SELECT LPAD('hi',4,'??');
+-------------------+
| LPAD('hi',4,'??') |
+-------------------+
| ??hi?????????????? |
+-------------------+

8.RPAD(str,len,padstr)
mysql> ? rpad
mysql> SELECT RPAD('hi',5,'?');
+------------------+
| RPAD('hi',5,'?') |
+------------------+
| hi??????????????? |
+------------------+

9.LTRIM(str)
mysql> ? ltrim
mysql> SELECT LTRIM('?? barbar');
+-------------------+
| LTRIM('?? barbar') |
+-------------------+
| barbar???????????? |
+-------------------+

10.RTRIM(str)
mysql> ? rtrim
mysql> SELECT RTRIM('barbar??? ');
+--------------------+
| RTRIM('barbar??? ') |
+--------------------+
| barbar????????????? |
+--------------------+

11.TRIM(str)
mysql> ? trim
mysql> SELECT TRIM('?? bar??? ');
+------------------+
| TRIM('?? bar??? ') |
+------------------+
| bar?????????????? |
+------------------+

12.REPEAT(str,count)
mysql> ? repeat function;
mysql> SELECT REPEAT('MySQL', 3);
+--------------------+
| REPEAT('MySQL', 3) |
+--------------------+
| MySQLMySQLMySQL???? |
+--------------------+

13.REPLACE(str,from_str,to_str)
mysql> ? replace
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
+-------------------------------------+
| REPLACE('www.mysql.com', 'w', 'Ww') |
+-------------------------------------+
| WwWwWw.mysql.com???????????????????? |
+-------------------------------------+

14.STRCMP(expr1,expr2)
mysql> ? strcmp
mysql> SELECT STRCMP('text', 'text2'),STRCMP('text2', 'text'),STRCMP('text', 'text');
+-----------------------+-----------------------+----------------------+
|STRCMP('text', 'text2')|STRCMP('text2', 'text')|STRCMP('text', 'text')|
+-----------------------+-----------------------+----------------------+
|???????????????????? -1 |?????????????????????? 1|????????????????????? 0|
+-----------------------+-----------------------+----------------------+

15.SUBSTRING
mysql> ? substring
SUBSTRING(str,pos), SUBSTRING(str FROM pos), SUBSTRING(str,pos,len),
SUBSTRING(str FROM pos FOR len)
mysql> SELECT SUBSTRING('Secooler',3,4), SUBSTRING('Secooler',6);
+---------------------------+-------------------------+
| SUBSTRING('Secooler',3,4) | SUBSTRING('Secooler',6) |
+---------------------------+-------------------------+
| cool?????????????????????? | ler????????????????????? |
+---------------------------+-------------------------+

二、数值函数
1.ABS(X) 取绝对值函数

mysql> ? abs
mysql> SELECT ABS(-32);
+----------+
| ABS(-32) |
+----------+
|??????? 32 |
+----------+

2.CEILING(X), CEIL(X) 取天棚函数
mysql> ? ceil
mysql> SELECT CEILING(1.23), CEIL(-1.23);
+---------------+-------------+
| CEILING(1.23) | CEIL(-1.23) |
+---------------+-------------+
|????????????? 2 |?????????? -1 |
+---------------+-------------+

3.FLOOR(X) 取地板函数
mysql> ? floor
mysql> SELECT FLOOR(1.23),FLOOR(-1.23);
+-------------+--------------+
| FLOOR(1.23) | FLOOR(-1.23) |
+-------------+--------------+
|??????????? 1 |??????????? -2 |
+-------------+--------------+

4.MOD(N,M), N % M, N MOD M 取模函数
mysql> ? mod
mysql> SELECT MOD(234, 10), 253 % 7, MOD(29,9), 29 MOD 9;
+--------------+---------+-----------+----------+
| MOD(234, 10) | 253 % 7 | MOD(29,9) | 29 MOD 9 |
+--------------+---------+-----------+----------+
|???????????? 4 |??????? 1 |????????? 2 |???????? 2 |
+--------------+---------+-----------+----------+

5.RAND(), RAND(N) 取0-1之间的随机数函数
mysql> ? rand
mysql> SELECT RAND(), RAND();
+------------------+-----------------+
| RAND()??????????? | RAND()?????????? |
+------------------+-----------------+
| 0.77874226009356 | 0.5317868818825 |
+------------------+-----------------+

6.TRUNCATE(X,D) 返回数字X被截断为D位小数的结果
mysql> ? truncate
mysql> SELECT TRUNCATE(1.223,1), TRUNCATE(1.999,1), TRUNCATE(-1.999,2);
+-------------------+-------------------+--------------------+
| TRUNCATE(1.223,1) | TRUNCATE(1.999,1) | TRUNCATE(-1.999,2) |
+-------------------+-------------------+--------------------+
|??????????????? 1.2 |??????????????? 1.9 |?????????????? -1.99 |
+-------------------+-------------------+--------------------+

三、日期和时间函数
1.CURDATE() 当前日期函数

mysql> ? curdate
mysql> SELECT CURDATE(),CURDATE() + 0;
+------------+---------------+
| CURDATE()?? | CURDATE() + 0 |
+------------+---------------+
| 2009-07-03 |?????? 20090703 |
+------------+---------------+

2.CURTIME() 当前时间函数
mysql> ? curtime
mysql> SELECT CURTIME(), CURTIME() + 0;
+-----------+---------------+
| CURTIME() | CURTIME() + 0 |
+-----------+---------------+
| 12:07:08?? |???????? 120708 |
+-----------+---------------+

3.NOW() 当前日期和时间函数
mysql> ? now
mysql> SELECT NOW(), NOW() + 0;
+---------------------+----------------+
| NOW()??????????????? | NOW() + 0?????? |
+---------------------+----------------+
| 2009-07-03 12:07:54 | 20090703120754 |
+---------------------+----------------+

4.UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date) 日期date的时间戳
mysql> ? unix_timestamp
mysql> SELECT UNIX_TIMESTAMP(), UNIX_TIMESTAMP('1981-02-15 23:23:00');
+------------------+---------------------------------------+
| UNIX_TIMESTAMP() | UNIX_TIMESTAMP('1981-02-15 23:23:00') |
+------------------+---------------------------------------+
|??????? 1246594366 |????????????????????????????? 351098580 |
+------------------+---------------------------------------+

5.FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format) 返回时间戳的日期值(UNIX_TIMESTAMP的反函数)
mysql> ? from_unixtime
mysql> SELECT FROM_UNIXTIME(1246594135),FROM_UNIXTIME(351098580);
+---------------------------+--------------------------+
| FROM_UNIXTIME(1246594135) | FROM_UNIXTIME(351098580) |
+---------------------------+--------------------------+
| 2009-07-03 12:08:55??????? | 1981-02-15 23:23:00?????? |
+---------------------------+--------------------------+

6.WEEK(date[,mode]) 返回所给日期是一年中的第几周
mysql> ? week
mysql> SELECT WEEK('1981-02-15');
+--------------------+
| WEEK('1981-02-15') |
+--------------------+
|?????????????????? 7 |
+--------------------+

7.YEAR(date)
mysql> ? year
mysql> SELECT YEAR('81-02-15');
+------------------+
| YEAR('81-02-15') |
+------------------+
|????????????? 1981 |
+------------------+

8.HOUR(time) 返回时间的小时信息
mysql> ? hour
mysql> SELECT HOUR('10:05:03');
+------------------+
| HOUR('10:05:03') |
+------------------+
|??????????????? 10 |
+------------------+

9.MINUTE(time) 返回时间的分钟信息
mysql> SELECT MINUTE('98-02-03 10:05:03');
+-----------------------------+
| MINUTE('98-02-03 10:05:03') |
+-----------------------------+
|??????????????????????????? 5 |
+-----------------------------+

10.MONTHNAME(date) 返回时间的完整月份名字
mysql> SELECT MONTHNAME('1981-02-15');
+-------------------------+
| MONTHNAME('1981-02-15') |
+-------------------------+
| February???????????????? |
+-------------------------+

11.DATE_FORMAT(date,format) 根据format格式date显示形式
mysql> ? date_format
mysql> SELECT DATE_FORMAT('1981-02-15 23:23:00', '%W %M %Y');
+------------------------------------------------+
| DATE_FORMAT('1981-02-15 23:23:00', '%W %M %Y') |
+------------------------------------------------+
| Sunday February 1981??????????????????????????? |
+------------------------------------------------+

12.DATE_ADD(date,INTERVAL expr type) 返回与所给日期date相差INTERVAL的时间段
mysql> select now() current, date_add(now(), INTERVAL 31 day) after31days;
+---------------------+---------------------+
| current????????????? | after31days????????? |
+---------------------+---------------------+
| 2009-07-03 12:34:15 | 2009-08-03 12:34:15 |
+---------------------+---------------------+

13.DATEDIFF(expr,expr2) 计算两个日期之间相差的天数
mysql> SELECT DATEDIFF(now(),'1981-02-15 23:23:00');
+---------------------------------------+
| DATEDIFF(now(),'1981-02-15 23:23:00') |
+---------------------------------------+
|????????????????????????????????? 10365 |
+---------------------------------------+

四、MySQL控制流程函数
首先创建演示表salary
mysql> use test;
mysql> create table salary (userid int, salary decimal(9,2));
mysql> insert into salary values (1,1000),(2,2000),(3,3000),(4,4000),(5,5000),(1,null);
mysql> select * from salary;
+--------+---------+
| userid | salary?? |
+--------+---------+
|?????? 1 | 1000.00 |
|?????? 2 | 2000.00 |
|?????? 3 | 3000.00 |
|?????? 4 | 4000.00 |
|?????? 5 | 5000.00 |
|?????? 1 |???? NULL |
+--------+---------+

1.IF(expr1,expr2,expr3)函数:如果expr1为真则返回expr2,否则返回expr3
mysql> ? IF FUNCTION
mysql> select IF( salary > 2000, 'High', 'Low') from salary;
+------------------------------------+
| if ( salary > 2000, 'High', 'Low') |
+------------------------------------+
| Low???????????????????????????????? |
| Low???????????????????????????????? |
| High??????????????????????????????? |
| High??????????????????????????????? |
| High??????????????????????????????? |
| Low???????????????????????????????? |
+------------------------------------+

2.IFNULL(expr1,expr2)函数:若expr1为NULL则返回expr2内容
mysql> ? IFNULL
mysql> select ifnull(salary,0) from salary;
+------------------+
| ifnull(salary,0) |
+------------------+
|?????????? 1000.00 |
|?????????? 2000.00 |
|?????????? 3000.00 |
|?????????? 4000.00 |
|?????????? 5000.00 |
|????????????? 0.00 |
+------------------+

3.NULLIF(expr1,expr2)函数:若expr1 = expr2则返回NULL,否则返回expr1
mysql> ? NULLIF
mysql> select nullif(salary,2000) from salary;
+---------------------+
| nullif(salary,2000) |
+---------------------+
|????????????? 1000.00 |
|???????????????? NULL |
|????????????? 3000.00 |
|????????????? 4000.00 |
|????????????? 5000.00 |
|???????????????? NULL |
+---------------------+

4.CASE函数
语法如下:
CASE?value?WHEN [compare_value] THEN result [WHEN?[compare_value]?THEN?result ...] [ELSE?result]?END

CASE WHEN?[condition]?THEN?result [WHEN [condition]?THEN?result ...] [ELSE?result]?END
mysql> ? CASE FUNCTION
mysql> select case when salary +-----------------------------------------------------+
| case when salary +-----------------------------------------------------+
| Low????????????????????????????????????????????????? |
| Low????????????????????????????????????????????????? |
| High???????????????????????????????????????????????? |
| High???????????????????????????????????????????????? |
| High???????????????????????????????????????????????? |
| High???????????????????????????????????????????????? |
+-----------------------------------------------------+

mysql> select case salary when 1000 then 'Low' when 2000 then 'Mid' else 'High' end from salary;
+-----------------------------------------------------------------------+
| case salary when 1000 then 'Low' when 2000 then 'Mid' else 'High' end |
+-----------------------------------------------------------------------+
| Low??????????????????????????????????????????????????????????????????? |
| Mid??????????????????????????????????????????????????????????????????? |
| High?????????????????????????????????????????????????????????????????? |
| High?????????????????????????????????????????????????????????????????? |
| High?????????????????????????????????????????????????????????????????? |
| High?????????????????????????????????????????????????????????????????? |
+-----------------------------------------------------------------------+

五、其他常用函数
1.DATABASE()函数:用于查询当前使用数据库的名字(类似Oracle的show user;)

mysql> ? database
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test??????? |
+------------+

2.VERSION()函数:用户查询所使用数据库的版本
mysql> ? version
mysql> SELECT VERSION();
+------------+
| VERSION()?? |
+------------+
| 5.0.22-log |
+------------+

3.USER()函数:查询当前登陆用户名
mysql> SELECT USER();
+----------------+
| USER()????????? |
+----------------+
| root@localhost |
+----------------+

4.INET_ATON(expr)函数:查询IP地址的网络字节序表示,反函数是INET_NTOA
mysql> SELECT INET_ATON('144.194.192.183');
+------------------------------+
| INET_ATON('144.194.192.183') |
+------------------------------+
|??????????????????? 2428682423 |
+------------------------------+

5.INET_NTOA(expr)函数:查询网络字节序代表的IP地址,是INET_ATON的反函数
mysql> ? inet_ntoa
mysql> SELECT INET_NTOA(2428682423);
+-----------------------+
| INET_NTOA(2428682423) |
+-----------------------+
| 144.194.192.183??????? |
+-----------------------+

六、小结
有事没事多请教一下帮助is a good idea. 也许这就是开源的好处,她会尽可能的考虑到您查询和参考的便利性。MySQL的函数还是很丰富的,以上实验用到的函数都是非常常用的。
这个小文儿,可以“一看了之”,只要你能想到MySQL提供的这些功能函数,check一下帮助系统,就什么都得到啦。
如果这些函数仍然没有满足您的求知欲望,敬请参考官方文档《第12章:函数和操作符》,地址为:http://dev.mysql.com/doc/refman/5.1/zh/functions.html

来源:http://my.oschina.net/taisha/blog/63451

?

?

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 Article

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

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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles