mysql常用函数 [转]
算数运算子 +, -, *, / 除于 0 会等于 NULL 。 比较运算子 任何东西跟 NULL 比较会等于 NULL 。 比较字符串通常不需要注意大小写,如果要比较大小写,用 BINARY 关键词。 程序代码 : SELECT * FROM department WHERE name = BINARY 'marketing'; 程序代码 :
算数运算子
+, -, *, /
除于 0 会等于 NULL。
比较运算子
任何东西跟 NULL 比较会等于 NULL。
比较字符串通常不需要注意大小写,如果要比较大小写,用 BINARY 关键词。
程序代码:
SELECT *
FROM department
WHERE name = BINARY 'marketing';
程序代码:
= 相等
!= 或 不相等
小于
小或等于
> 大于
>= 大或等于
n BETWEEN
min AND max 测试范围
n IN (set) set 可以是一系列的数值或子查询。
可以比较 NULL,如果比较两个 NULL 会传回 1。
n IS NULL 测试 n 是否 NULL。
ISNULL(n) 测试 n 是否 NULL。
逻辑运算子
传回的值有 1 (true), 0 (false, 或 NULL。
任何不是 0 或不是 NULL 的值都是 true。
程序代码:
AND 或 && n && m 和
true && true = true
false && anything = false
其它会等于 NULL
OR 或 || n || m 或
true || anything = true
NULL || false = NULL
NULL || NULL = NULL
false || false = false
NOT 或 ! NOT n 不是
!true = false
!false = true
!NULL = NULL
XOR n XOR m 不包含的 OR
true XOR true = false
true XOR false = true
false XOR true = true
NULL XOR n = NULL
n XOR NULL = NULL
控制流向的函式
IF (e1, e2, e3)
如果 e1 是 true,IF 会传回 e2,不然会传回 e3。
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
程序代码:
SELECT workdate, CASE
WHEN workdate
WHEN workdate
ELSE "current"
END
FROM assignment;
字符串函式
concat(s1, s2, ...) - 连接字符串 s1 和 s2...
conv(n, original_base, new_base) - 转换数字 n 从本来的 base 到新的 base
length(s) - 找字符串的长度
load_file(filename) - 依照字符串传回档案的内容
locate(needle, haystack, position) - 传回 needle 字符串的开始位置,在 haystack 字符串里,从 position 开始
lower(s) and upper(s) - 转换 s 到小写或大写
quote(s) - 跳脱 s 字符串,让它可以安全输入数据库
replace(target, find, replace) - 传回 target 字符串,将 find 字符串覆盖成 replace 字符串
soundex(s) - 传回跟 s 类似的 soundex 字符串。soundex 是字符串的发音
substring(s, position, length) - 传回字符串里的字符串,s 是本来的字符串,position 是开始的位置,length 是传回的字数
trim(s) - 移除开头跟字尾的空格符。也可以用 rtrim() 或 ltrim()
字符串比较的函式
LIKE - 利用通配字符来进行比较
RLIKE - 利用正规表示法来进行比较
STRCMP - 比较字符串,类似 C 里面的 strcmp()
MATCH - 进行 full-text 比较
利用通配字符来进行比较
程序代码:
SELECT *
FROM department
WHERE name LIKE '%research%';
% 会吻合任何字数,包括 0。
_ 会吻合单一字数, _at 会吻合 cat, mat, bat...
利用正规表示法来进行比较
RLIKE 可以用来吻合正规表示法。
'cat' 会吻合 'catacomb' 和 'the cat sat on the mat'。
如果只要吻合 'cat',用 '^cat$'。
^ 表示在吻合的字符串开头是 'cat'。
$ 表示在吻合的字符串最后是 'cat'。
. 可以用来代表通配字符,'.at' 会吻合 'cat', 'bat', 'mat'。
* 表示字符可以出现零或多次,'n*' 会吻合 '', 'nn', 'nnn'。
() 会归类字符,'(cat)*' 会吻合 '', 'cat', 'catcat', 'catcatcat'。
.* 会吻合任何字或字符串。
+ 表示在他之后的字或字符串会重复一或多次。
? 表示吻合一或零次。
列出特定的范围,'(cat)(2,4)' 会吻合 'catcat', 'catcatcat', 和 'catcatcatcat'。
[] 可以列出一系列的文字,'[a-z]' 会吻合任何字母,'[a-z]*' 会吻合任何数量的字母。
文字类别,[[:alnum:]] 会吻合任何字母和数字的文字。
程序代码:
SELECT *
FROM department
WHERE name RLIKE 'an';
这会吻合全部有包括 'an' 的部门。
用 STRCMP() 来比较字符串
STRCMP(s1, s2)
如果字符串相同会传回 0,-1 如果 s1 比 s2 小 (s1 比 s2 早出现)。
1 如果 s1 比 s2 大 (s1 比 s2 晚出现)。
程序代码:
SELECT STRCMP('cat', 'cat');
// 传回 0
SELECT STRCMP('cat', 'dog');
// 传回 -1
SELECT STRCMP('cat', 'ant');
// 传回 1
数字函式
abs(n) |
传回 n 的正数 |
ceiling(n) |
n rounded up to the nearest integer |
floor(n) |
n rounded down to the nearest integer |
mod(n,m) 和 div |
这两个函式会将 n 除于 m。div 传回商数,mod() 会传回剩余数。 |
power(n,m) |
n to the power of m |
rand(n) |
传回 0 到 1 的随意数。n 可以不提供,如果提供会用来产生随意数。 |
round(n[,d]) |
n rounded to the nearest integer. if supply d, n will be rounded to d decimal places. |
sqrt(n) |
传回 n 的平方 |
mod() 可以是 mod(9,2) 或 9 mod 2 或 9 % 2。
div 只能用 9 div 2。
程序代码:
SELECT 9 mod 2;
// 传回 1
SELECT 9 div 2;
// 传回 4
日期和时间函式
adddate(date, INTERVAL n type) 和 subdate(date, INTERVAL n type)
这些函式可以用来增加和减少日期。从 date 的日期开始计算,然后增加或减少 INTERVAL 后的日期范围。
你必须提供 n 和他的类型。类型可以是 SECOND, MINUTE, HOUR, DAY, MONTH, YEAR, MINUTE:SECOND (m:s),
HOUR:MINUTE (h:m), DAY_HOUR(d h), YEAR_MONTH (y-m), HOUR_SECOND (h:m:s), DAY_MINUTE (d h:m),
DAY_SECOND (d h:m:s)。
curdate(), curtime(), now()
这些函式会传回目前的日期,目前的时间,还有时间和日期。
date_format(date, format) 和 time_format(time, format)
这些可以用来改变日期和时间的格式。
你必须提供格式的字符串,譬如说 date_format(workdate, '%W %D of %M, %Y'),
这会给你 'Monday 16th of June, 2003)。
全部可用的格式请看 MySQL 的手册。
dayname(date)
传回日期的名称,例如 Monday。
extract(type FROM date)
传回 date 的日期,譬如说 YEAR,他就会传回那个日期的年数。
函式可用的类型跟 adddate() 和 subdate() 的类型相同。
unix_timestamp([date])
传回目前的 Unix 时间。如果跟 date 一起使用,传回的日期会从 date 的日期开始计算。
范例:
程序代码:
SELECT adddate("1999-01-01", INTERVAL "1-6" YEAR_MONTH);
// 传回 2000-07-01
SELECT unix_timestamp(adddate("1999-01-01", INTERVAL "1-6" YEAR_MONTH);
// 传回 962373600
可以用 PHP 的 date() 函式来将 Unix 的时间格式化。
cast 函式
cast 可以用来改变类型,用 cast() 和 convert() 函式。
两个函式相同,但是不同的语法构造。
cast(expression AS type)
convert(expression, type)
cast() 是依照 ANSI 的规格,convert() 是依照 ODBC 的规格。
类型可以是 BINARY, CHAR, DATE, DATETIME, SIGNED (INTEGER), 和 UNSIGNED (INTEGER)。
通常 MySQL 会自动帮你改变类型。
其它函式
benchmark(count, expression)
这个函式是用来测试查询的速度,只会传回 0 值。
encrypt(s[,salt])
将 s 加密用 Unix 的加密系统。salt 字符串是两个字符的字符串,可以不用。
如果没有 crypt 功能,像在 Windows 系统,这个函式会传回 NULL。
found_rows()
传回字段的数量,如果没有用 LIMIT。
只有在 SELECT 里使用 SQL_CALC_FOUND_ROWS 才能用这个函式。
last_insert_id()
传回最后产生的 AUTO_INCREMENT 值。
md5(s)
传回 128bit MD5 的加密字符串。
password(s)
计算 s 的密码,不建议用这个函式来储存密码。
用于 GROUP BY 的函式
这些还是专门为了 GROUP BY 而写的。
avg(column) - 传回字段的平均值
count(column) - 传回字段的数量
min(column) - 传回字段的最小值
max(column) - 传回字段的最大值
std(column) - 传回字段的标准差
sum(column) - 传回字段的总数

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

The methods for viewing SQL database errors are: 1. View error messages directly; 2. Use SHOW ERRORS and SHOW WARNINGS commands; 3. Access the error log; 4. Use error codes to find the cause of the error; 5. Check the database connection and query syntax; 6. Use debugging tools.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.
