Detailed explanation of examples of functions and predicates in MySQL

零下一度
Release: 2017-06-28 10:29:41
Original
1204 people have browsed it

It has been covered in the previous article, so I will reorganize it here as a summary.

1. Functions

1. Arithmetic functions

NUMERIC is a data type supported by most DBMS, through NUMBERIC (whole digits, decimal digits) form to specify the size of the numeric value.

-- +-*/ --余数 SELECT n, p, MOD(n, p) AS mod_col FROM SampleMath; --绝对值 SELECT m, ABS(m) AS abs_col FROM SampleMath; --四舍五入 SELECT m, n, ROUND(m, n) AS round_col FROM SampleMath;
Copy after login

2. String function

--字符串拼接 SELECT str1, str2, str3, CONCAT(str1, str2, str3) AS str_concat FROM SampleStr --字符串长度 SELECT str1, LENGTH(str1) AS len_str FROM SampleStr; --大小写转换 SELECT str1, UPPER/LOWER(str1) AS low_str FROM SampleStr WHERE str1 IN ('ABC', 'aBC', 'abc') --字符串替换str1中的str2换为str3 SELECT str1, str2, str3, REPLACE(str1, str2, str3) AS rep_str FROM SampleStr; --字符串截取 FROM截取的起始位置FOR截取的字符数 SELECT str1, SUBSTRING(str1 FROM 3 FOR 2) AS sub_str FROM SampleStr;
Copy after login

3. Date function

--当前日期 SELECT CURRENT_DATE; --当前时间 SELECT CURRENT_TIME; --当前时间戳 SELECT CURRENT_TIMESTAMP; --截取日期元素 SELECT CURRENT_TIMESTAMP, EXTRACT(YEAR FROM CURRENT_TIMESTAMP) AS year, EXTRACT(MONTH FROM CURRENT_TIMESTAMP) AS month, EXTRACT(DAY FROM CURRENT_TIMESTAMP) AS day, EXTRACT(HOUR FROM CURRENT_TIMESTAMP) AS hour, EXTRACT(MINUTE FROM CURRENT_TIMESTAMP) AS minute, EXTRACT(SECOND FROM CURRENT_TIMESTAMP) AS second;
Copy after login

4. Conversion function

--类型转换 SELECT CAST('0001' AS SIGNED INTEGER) AS int_col; SELECT CAST('2009-12-14' AS DATE) AS date_col; --将 NULL 转换为其他值 --返回可变参数中左侧开始第 1 个不是 NULL 的值 SELECT COALESCE(NULL, 1) AS col_1, COALESCE(NULL, 'test', NULL) AS col_2, COALESCE(NULL, NULL, '2009-11-01') AS col_3;
Copy after login

5. Aggregation function

COUNT, SUM, AVG, MAX, MIN

More functions and operators

2. Predicate

The function of the predicate is to "determine whether there is a condition that satisfies a certain condition" Record". Returns true (TRUE) if such a record exists, returns false (FALSE) if it does not exist.

--部分一致查询 LIKE --范围查询 BETWEEN --判断是否为NULL IS NULL、IS NOT NULL --OR 的简便用法 IN --谓词的主语是“记录”...没懂! EXISTS
Copy after login

3. CASE expression

--格式 CASE WHEN <求值表达式> THEN <表达式> WHEN <求值表达式> THEN <表达式> WHEN <求值表达式> THEN <表达式> . . . ELSE <表达式> END
Copy after login
-- 使用搜索CASE表达式的情况 SELECT product_name, CASE WHEN product_type = '衣服' THEN 'A :' | |product_type WHEN product_type = '办公用品' THEN 'B :' | |product_type WHEN product_type = '厨房用具' THEN 'C :' | |product_type ELSE NULL END AS abc_product_type FROM Product; -- 使用简单CASE表达式的情况 SELECT product_name, CASE product_type WHEN '衣服' THEN 'A :' || product_type WHEN '办公用品' THEN 'B :' || product_type WHEN '厨房用具' THEN 'C :' || product_type ELSE NULL END AS abc_product_type FROM Product;
Copy after login
--使用IF代替CASE表达式 SELECT product_name, IF( IF( IF(product_type = '衣服', CONCAT('A :', product_type), NULL) IS NULL AND product_type = '办公用品', CONCAT('B :', product_type), IF(product_type = '衣服', CONCAT('A :', product_type), NULL)) IS NULL AND product_type = '厨房用具', CONCAT('C :', product_type), IF( IF(product_type = '衣服', CONCAT('A :', product_type), NULL) IS NULL AND product_type = '办公用品', CONCAT('B :', product_type), IF(product_type = '衣服', CONCAT('A :', product_type), NULL))) AS abc_product_type FROM Product;
Copy after login

The above is the detailed content of Detailed explanation of examples of functions and predicates in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!