- 浏览: 56592 次
- 性别:
- 来自: 北京
文章分类
最新评论
一、常用字符串函数
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 <= 2000 then 'Low' else 'High' end from salary;
+-----------------------------------------------------+
| case when salary <= 2000 then 'Low' else 'High' end |
+-----------------------------------------------------+
| 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
好运!
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 <= 2000 then 'Low' else 'High' end from salary;
+-----------------------------------------------------+
| case when salary <= 2000 then 'Low' else 'High' end |
+-----------------------------------------------------+
| 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
好运!
相关推荐
### MySQL常用函数详解 #### 数学函数 - **ABS(x)**: 返回参数`x`的绝对值。 - **BIN(x)**: 将十进制数字`x`转换为二进制字符串表示。 - **CEILING(x)**: 返回不小于`x`的最小整数值。 - **EXP(x)**: 返回`e`的`x`...
MySQL常用函数 MySQL常用函数
### MySQL常用函数详解 #### 一、数学函数 在MySQL中,数学函数是处理数值数据的基本工具之一。这些函数能够帮助我们执行简单的算术运算、数值转换以及进行数学计算。 - **ABS(x)**:返回`x`的绝对值。例如,`ABS...
包含了MySQL常用的所有函数及详解。IFNULL(expr1,expr2),FROM_UNIXTIME。
Mysql常用函数列表 CONCAT (string2 [,... ]) //连接字串 INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0 …… 数学类 ABS (number2 ) //绝对值 …… 日期...
### MySQL常用函数详解 #### 一、数学函数 数学函数是MySQL中经常使用的一类函数,主要用于处理各种类型的数字,如整数、浮点数等。以下是一些常用的数学函数: 1. **ABS(X)**: 返回`X`的绝对值。 - 示例:`...
### MySQL 常用函数及概念详解 #### 一、MySQL 数据库优化——重点函数与概念应用 在处理大规模数据时,对MySQL数据库进行优化至关重要。优化不仅能够提高查询速度,减少服务器负担,还能有效提升应用程序的整体...
mysql 函数整理,方便使用。。。。。。。。。。。。。。。。。。。。。
MySQL 常用函数精编版 在 MySQL 中,函数是指对输入参数值返回一个具有特定关系的值,主要分为数学函数、字符串函数、日期和时间函数、条件判断函数、系统信息和加密函数等。以下是 MySQL 中一些常用的函数: 数学...
这篇文本“mysql常用函数.txt”很可能是列举了一些在MySQL中经常用到的函数,这对于理解和操作MySQL数据库至关重要。以下是对MySQL中一些关键函数的详细介绍: 1. **SELECT函数**:这是查询数据的基本函数,用于从...
掌握这些MySQL常用函数,可以极大地提升你的SQL编写能力,使你在数据查询和处理时更加得心应手。在实际工作中,可以根据需求灵活运用这些函数,创建复杂的查询语句,实现高效的数据操作。不断实践和学习,你将成为...
mysql常用函数
mysql常用函数大全,不用每次去网上查,快速便捷
Mysql的常用函数整体, 从网上收集的一些常用函数, 进行汇总整体成文档 MySQL控制流函数: CASE WHEN[test1] THEN [result1]...ELSE [default] END如果testN是真,则返回resultN,否则返回default CASE [test] WHEN...
以下是关于MySQL常用函数的一些详细说明: **一、控制流程函数** 1. **CASE...WHEN...END** 语句:这是一个条件判断语句,可以根据不同的条件返回不同的结果。在给定的示例中,Eg1 判断1是否大于0,如果是则返回'...
以下是 MySQL 常用函数的详细说明: 1. `mysql_affected_rows`:这个函数返回上一次操作(如INSERT、UPDATE、DELETE)影响的记录行数,可以帮助判断操作是否成功。 2. `mysql_change_user`:允许在已建立的连接上...
MySQL常用函数汇总.md