Home >Database >Mysql Tutorial >Detailed explanation of examples of functions and predicates in MySQL
It has been covered in the previous article, so I will reorganize it here as a summary.
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;
--字符串拼接 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;
--当前日期 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;
--类型转换 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;
COUNT, SUM, AVG, MAX, MIN
More functions and operators
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
--格式 CASE WHEN <求值表达式> THEN <表达式> WHEN <求值表达式> THEN <表达式> WHEN <求值表达式> THEN <表达式> . . . ELSE <表达式> END
-- 使用搜索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;
--使用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;
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!