MySQL函数基础25字中文指南(mysql中函数基础入门)
MySQL函数基础:25字中文指南
MySQL是一种常用的关系型数据库管理系统,拥有丰富的函数库来方便开发者进行各种数据处理操作。本文将为大家提供一个MySQL函数25字中文指南,让你快速了解MySQL函数的基础用法。
1. AVG:计算平均值
SELECT AVG(score) FROM table;
2. CONCAT:合并字符串
SELECT CONCAT(firstname, ‘ ‘, lastname) FROM table;
3. COUNT:计算行数
SELECT COUNT(*) FROM table;
4. DATE_FORMAT:格式化日期
SELECT DATE_FORMAT(date, ‘%Y-%m-%d’) FROM table;
5. IF:条件判断
SELECT IF(score >= 60, ‘及格’, ‘不及格’) FROM table;
6. LENGTH:计算字符串长度
SELECT LENGTH(name) FROM table;
7. MAX:找出最大值
SELECT MAX(score) FROM table;
8. MIN:找出最小值
SELECT MIN(score) FROM table;
9. RAND:生成随机数
SELECT RAND() FROM table;
10. ROUND:四舍五入
SELECT ROUND(score, 2) FROM table;
11. SUBSTR:截取字符串
SELECT SUBSTR(name, 1, 3) FROM table;
12. SUM:计算总和
SELECT SUM(score) FROM table;
13. TRIM:去除空格
SELECT TRIM(name) FROM table;
14. UCASE:转换为大写
SELECT UCASE(name) FROM table;
15. LCASE:转换为小写
SELECT LCASE(name) FROM table;
16. NOW:获取当前时间
SELECT NOW() FROM table;
17. YEAR:获取年份
SELECT YEAR(date) FROM table;
18. MONTH:获取月份
SELECT MONTH(date) FROM table;
19. DAY:获取日期
SELECT DAY(date) FROM table;
20. CONCAT_WS:合并字符串并去掉空值
SELECT CONCAT_WS(‘ ‘, firstname, lastname) FROM table;
21. GROUP_CONCAT:按分组合并字符串
SELECT GROUP_CONCAT(name SEPARATOR ‘,’) FROM table GROUP BY score;
22. IFNULL:替换空值
SELECT IFNULL(name, ‘未知’) FROM table;
23. CASE:多条件判断
SELECT CASE WHEN score >= 90 THEN ‘优秀’ WHEN score >= 60 THEN ‘及格’ ELSE ‘不及格’ END FROM table;
24. INSTR:查找字符串位置
SELECT INSTR(name, ‘ll’) FROM table;
25. REPLACE:替换字符串
SELECT REPLACE(name, ‘ll’, ‘rr’) FROM table;
以上25个MySQL函数基础指南可以让你更加熟练地使用MySQL进行数据处理,极大地提高你的开发效率。
代码演示:
SELECT AVG(score) FROM student;
SELECT CONCAT(firstname, ‘ ‘, lastname) FROM student;
SELECT COUNT(*) FROM student;
SELECT DATE_FORMAT(date, ‘%Y-%m-%d’) FROM student;
SELECT IF(score >= 60, ‘及格’, ‘不及格’) FROM student;
SELECT LENGTH(name) FROM student;
SELECT MAX(score) FROM student;
SELECT MIN(score) FROM student;
SELECT RAND() FROM student;
SELECT ROUND(score, 2) FROM student;
SELECT SUBSTR(name, 1, 3) FROM student;
SELECT SUM(score) FROM student;
SELECT TRIM(name) FROM student;
SELECT UCASE(name) FROM student;
SELECT LCASE(name) FROM student;
SELECT NOW() FROM student;
SELECT YEAR(date) FROM student;
SELECT MONTH(date) FROM student;
SELECT DAY(date) FROM student;
SELECT CONCAT_WS(‘ ‘, firstname, lastname) FROM student;
SELECT GROUP_CONCAT(name SEPARATOR ‘,’) FROM student GROUP BY score;
SELECT IFNULL(name, ‘未知’) FROM student;
SELECT CASE WHEN score >= 90 THEN ‘优秀’ WHEN score >= 60 THEN ‘及格’ ELSE ‘不及格’ END FROM student;
SELECT INSTR(name, ‘ll’) FROM student;
SELECT REPLACE(name, ‘ll’, ‘rr’) FROM student;