常用函数
字符串函数:
- 常用的字符串函数
- CONCAT(s1, s2, ...):依次连接字符串。
mysql> SELECT CONCAT('aa', 'bb'), CONCAT('abc', 'de', 'fgh');+--------------------+----------------------------+| CONCAT('aa', 'bb') | CONCAT('abc', 'de', 'fgh') |+--------------------+----------------------------+| aabb | abcdefgh |+--------------------+----------------------------+
- INSERT(str, x, y, instr):将str从x位置开始的y个字符串替换为instr,索引从1开始。
mysql> SELECT INSERT('ilike2014', 2, 4, 'enjoy');+------------------------------------+| INSERT('ilike2014', 2, 4, 'enjoy') |+------------------------------------+| ienjoy2014 |+------------------------------------+
- LPAD(str, n, pad):str长度不足n,则左填充pad。
mysql> SELECT LPAD('XXX', 12, '01');+-----------------------+| LPAD('XXX', 12, '01') |+-----------------------+| 010101010XXX |+-----------------------+
- SUBSTRING(str, x, y): 求子串str[x..y],索引从1开始。
mysql> SELECT SUBSTRING('abcdefg', 1, 3);+----------------------------+| SUBSTRING('abcdefg', 1, 3) |+----------------------------+| abc |+----------------------------+
数值函数:
- 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 |+-----------------+--------------------+
日期和时间函数:
- 常用的日期时间函数:
范例:
-- 列举当前时间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 |+-------------+-------------+
- 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 |+-----------------------------------------+
- 有关时间计算
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 |+---------------------+---------------------+---------------------+---------------------+
流程函数:
- 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 |+-----------------------------------------------------------------------+
其他常用函数:
- 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 |+----------------------------------+MySQL更多函数可参考:
http://dev.mysql.com/doc/refman/5.7/en/functions.html
不吝指正。

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
