Table of Contents
常用函数
字符串函数:
数值函数:
日期和时间函数:
流程函数:
其他常用函数:
Home Database Mysql Tutorial MySQL管理与优化(4)_MySQL

MySQL管理与优化(4)_MySQL

Jun 01, 2016 pm 01:09 PM

常用函数

字符串函数:

  • 常用的字符串函数

    

  • CONCAT(s1, s2, ...):依次连接字符串。  
mysql> SELECT CONCAT('aa', 'bb'), CONCAT('abc', 'de', 'fgh');+--------------------+----------------------------+| CONCAT('aa', 'bb') | CONCAT('abc', 'de', 'fgh') |+--------------------+----------------------------+| aabb               | abcdefgh                   |+--------------------+----------------------------+
Copy after login
  • INSERT(str, x, y, instr):将str从x位置开始的y个字符串替换为instr,索引从1开始。
mysql> SELECT INSERT('ilike2014', 2, 4, 'enjoy');+------------------------------------+| INSERT('ilike2014', 2, 4, 'enjoy') |+------------------------------------+| ienjoy2014                         |+------------------------------------+
Copy after login
  • LPAD(str, n, pad):str长度不足n,则左填充pad。
mysql> SELECT LPAD('XXX', 12, '01');+-----------------------+| LPAD('XXX', 12, '01') |+-----------------------+| 010101010XXX          |+-----------------------+
Copy after login
  • SUBSTRING(str, x, y): 求子串str[x..y],索引从1开始。
mysql> SELECT SUBSTRING('abcdefg', 1, 3);+----------------------------+| SUBSTRING('abcdefg', 1, 3) |+----------------------------+| abc                        |+----------------------------+
Copy after login

数值函数:

  • MySQL常用的数值函数:

范例:

mysql> SELECT ABS(0.8), ABS(-0.8), CEIL(-0.8), CEIL(0.8), FLOOR(-0.8), FLOOR(0.8), MOD(15,10), MOD(1,11), MOD(NULL,10);+----------+-----------+------------+-----------+-------------+------------+------------+-----------+--------------+| ABS(0.8) | ABS(-0.8) | CEIL(-0.8) | CEIL(0.8) | FLOOR(-0.8) | FLOOR(0.8) | MOD(15,10) | MOD(1,11) | MOD(NULL,10) |+----------+-----------+------------+-----------+-------------+------------+------------+-----------+--------------+|      0.8 |       0.8 |          0 |         1 |          -1 |          0 |          5 |         1 |         NULL |+----------+-----------+------------+-----------+-------------+------------+------------+-----------+--------------+mysql> SELECT ROUND(1.235, 2), TRUNCATE(1.235, 2);+-----------------+--------------------+| ROUND(1.235, 2) | TRUNCATE(1.235, 2) |+-----------------+--------------------+|            1.24 |               1.23 |+-----------------+--------------------+
Copy after login

日期和时间函数:

  • 常用的日期时间函数:

范例:

-- 列举当前时间mysql> SELECT CURDATE(), CURTIME(), NOW();+------------+-----------+---------------------+| CURDATE()  | CURTIME() | NOW()               |+------------+-----------+---------------------+| 2014-06-12 | 11:54:22  | 2014-06-12 11:54:22 |+------------+-----------+---------------------+-- 返回Unix时间戳mysql> SELECT UNIX_TIMESTAMP(now());+-----------------------+| UNIX_TIMESTAMP(now()) |+-----------------------+|            1402545326 |+-----------------------+-- 返回Unix时间戳对应的日期mysql> SELECT UNIX_TIMESTAMP(now());+-----------------------+| UNIX_TIMESTAMP(now()) |+-----------------------+|            1402545326 |+-----------------------+-- 现在是哪周,哪年mysql> SELECT WEEK(now()), YEAR(now());+-------------+-------------+| WEEK(now()) | YEAR(now()) |+-------------+-------------+|          23 |        2014 |+-------------+-------------+
Copy after login
  • MySQL中的日期时间格式化:

如:

mysql> SELECT DATE_FORMAT(now(), '%Y-%m-%d %H:%m:%s');+-----------------------------------------+| DATE_FORMAT(now(), '%Y-%m-%d %H:%m:%s') |+-----------------------------------------+| 2014-06-12 12:06:13                     |+-----------------------------------------+
Copy after login
  • 有关时间计算

MySQL中的日期时间间隔类型有:

范例:

mysql> SELECT now() current, DATE_ADD(now(), INTERVAL 31 DAY) after31days, DATE_ADD(now(), INTERVAL -7 DAY) before7days, DATE_ADD(now(), INTERVAL '1_2' YEAR_MONTH) after_1year_2month;+---------------------+---------------------+---------------------+---------------------+| current             | after31days         | before7days         | after_1year_2month  |+---------------------+---------------------+---------------------+---------------------+| 2014-06-12 22:48:49 | 2014-07-13 22:48:49 | 2014-06-05 22:48:49 | 2015-08-12 22:48:49 |+---------------------+---------------------+---------------------+---------------------+
Copy after login

流程函数:

  • MySQL中的流程函数:

范例:

-- 初始化数据mysql> CREATE TABLE salary (userid int, salary decimal(9, 2));Query OK, 0 rows affected (0.06 sec)mysql> INSERT INTO salary VALUES(1, 1000), (2, 2000), (3, 3000), (4, 4000), (5, 5000), (1, NULL);Query OK, 6 rows affected (0.02 sec)Records: 6  Duplicates: 0  Warnings: 0mysql> SELECT * FROM salary;+--------+---------+| userid | salary  |+--------+---------+|      1 | 1000.00 ||      2 | 2000.00 ||      3 | 3000.00 ||      4 | 4000.00 ||      5 | 5000.00 ||      1 |    NULL |+--------+---------+-- 根据薪水高低判断薪水mysql> SELECT IF(salary>2000, 'high', 'low') FROM salary;+--------------------------------+| IF(salary>2000, 'high', 'low') |+--------------------------------+| low                            || low                            || high                           || high                           || high                           || low                            |+--------------------------------+-- IFNULL使用mysql> SELECT IFNULL(salary, 0) FROM salary;+-------------------+| IFNULL(salary, 0) |+-------------------+|           1000.00 ||           2000.00 ||           3000.00 ||           4000.00 ||           5000.00 ||              0.00 |+-------------------+-- CASE WHEN使用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                                                                  |+-----------------------------------------------------------------------+
Copy after login

其他常用函数:

  • MySQL中其他常用函数:

范例:

mysql> SELECT DATABASE(), VERSION(), USER();+------------+-----------+----------------+| DATABASE() | VERSION() | USER()         |+------------+-----------+----------------+| test       | 5.6.14    | root@localhost |+------------+-----------+----------------+mysql> SELECT DATABASE(), VERSION(), USER();+------------+-----------+----------------+| DATABASE() | VERSION() | USER()         |+------------+-----------+----------------+| test       | 5.6.14    | root@localhost |+------------+-----------+----------------+mysql> SELECT INET_ATON('192.168.141.129');+------------------------------+| INET_ATON('192.168.141.129') |+------------------------------+|                   3232271745 |+------------------------------+-- 41位加密密码mysql> SELECT PASSWORD('111111');+-------------------------------------------+| PASSWORD('111111')                        |+-------------------------------------------+| *FD571203974BA9AFE270FE62151AE967ECA5E0AA |+-------------------------------------------+-- MD5值计算mysql> SELECT MD5('111111');+----------------------------------+| MD5('111111')                    |+----------------------------------+| 96e79218965eb72c92a549dd5a330112 |+----------------------------------+
Copy after login
MySQL更多函数可参考:

http://dev.mysql.com/doc/refman/5.7/en/functions.html

不吝指正。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do you represent relationships using foreign keys? How do you represent relationships using foreign keys? Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

See all articles