search
HomeDatabaseMysql Tutorial贤者时间太久了么?--MySQL继续玩_MySQL

1、MySQL

 

-----运算符和函数-----

 

字符函数,数值运算符,比较运算等

 

----字符函数

 

---

 

CONCAT()--字符连接

 

CONCAT_WS()--使用指定的分隔符进行字符连接

 

mysql> SELECT CONCAT('imooc','-','MySQL');

+-----------------------------+

| CONCAT('imooc','-','MySQL') |

+-----------------------------+

| imooc-MySQL |

+-----------------------------+

 

mysql> USE TEST;

Database changed

mysql> SELECT * FROM TEST;

+----+----------+

| id | username |

+----+----------+

| 1 | Tom |

| 2 | Ben |

+----+----------+

2 rows in set (0.02 sec)

 

mysql> SELECT CONCAT(id,username) AS fullname FROM test;

+----------+

| fullname |

+----------+

| 1Tom |

| 2Ben |

+----------+

 

以上是CONCAT()的实例,而CONCAT_WS()如下

 

mysql> SELECT CONCAT_WS('-',id,username) FROM test;

+----------------------------+

| CONCAT_WS('-',id,username) |

+----------------------------+

| 1-Tom |

| 2-Ben |

+----------------------------+

 

CONCAT_WS()需要至少三个参数,第一个是分隔符,后面才是需要连接的东西

 

---

 

FORMAT(),数字格式化

 

mysql> SELECT FORMAT(234234.23423,2);

+------------------------+

| FORMAT(234234.23423,2) |

+------------------------+

| 234,234.23 |

+------------------------+

 

就是标准化,当然是外国人那一套

 

---

 

LOWER()&UPPER(),大小写变换

 

---

 

LEFT()&RIGHT(),获取左右侧字符

 

需要两位参数,从哪个字段取,从第几位开始的左右侧

 

mysql> SELECT UPPER(LEFT('andy',1));

+-----------------------+

| UPPER(LEFT('andy',1)) |

+-----------------------+

| A |

+-----------------------+

 

---

 

LENGTH(),获取字符串长度。注意,空格也要算在内

 

---

 

LTRIM(),RTRIM(),TRIM(),删除前/后/前后的空格

 

TRIM还能删除其他字符

 

mysql> SELECT TRIM(LEADING '!' FROM '!!!ANDY!!');

+------------------------------------+

| TRIM(LEADING '!' FROM '!!!ANDY!!') |

+------------------------------------+

| ANDY!! |

+------------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT TRIM(BOTH '!' FROM '!!!ANDY!!');

+---------------------------------+

| TRIM(BOTH '!' FROM '!!!ANDY!!') |

+---------------------------------+

| ANDY |

+---------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT TRIM(TRAILING '!' FROM '!!!ANDY!!');

+-------------------------------------+

| TRIM(TRAILING '!' FROM '!!!ANDY!!') |

+-------------------------------------+

| !!!ANDY |

+-------------------------------------+

1 row in set (0.00 sec)

 

---

 

REPLACE()

 

mysql> SELECT REPLACE('!!ANDY!SDL!!','!','');

+--------------------------------+

| REPLACE('!!ANDY!SDL!!','!','') |

+--------------------------------+

| ANDYSDL |

+--------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT REPLACE('!!ANDY!SDL!!','!','LALAL');

+-------------------------------------+

| REPLACE('!!ANDY!SDL!!','!','LALAL') |

+-------------------------------------+

| LALALLALALANDYLALALSDLLALALLALAL |

+-------------------------------------+

1 row in set (0.00 sec)

 

可以看到,替换是比较灵活的

 

---

 

SUBSTRING()

 

mysql> SELECT SUBSTRING('MYSQL','1','2');

+----------------------------+

| SUBSTRING('MYSQL','1','2') |

+----------------------------+

| MY |

+----------------------------+

1 row in set (0.00 sec)

 

注意mysql是从1开始,不是从0开始数数

 

---

 

做匹配

 

mysql> SELECT 'mysql' LIKE 'M%';

+-------------------+

| 'mysql' LIKE 'M%' |

+-------------------+

| 1 |

+-------------------+

1 row in set (0.00 sec)

 

mysql> SELECT * FROM test WHERE username LIKE '%m%';

+----+----------+

| id | username |

+----+----------+

| 1 | Tom |

+----+----------+

 

这里,%代表任意,类似window中的*

 

_代表任意一个字符

 

----数值运算符以及函数

 

几个很通用的函数简单的介绍下

 

CEIL()-向上取整---------FLOOR()-向下取整

 

DIV()-整数除法,也就是结果只有整数

 

MOD()-取余数

 

POWER()-幂运算

 

ROUND()-四舍五入

 

TRUNCATE()-数字截取(不再四舍五入)

 

不是两位参数,就是一位参数,大家自己试试哈

 

----比较运算符与函数

 

---

 

[NOT] BETWEEN...AND...

 

mysql> SELECT 123 BETWEEN 2 AND 123123;

+--------------------------+

| 123 BETWEEN 2 AND 123123 |

+--------------------------+

| 1 |

+--------------------------+

 

---

 

[NOT] IN(),给定几个区间来做判断

 

mysql> SELECT 123 IN(123,23,12);

+-------------------+

| 123 IN(123,23,12) |

+-------------------+

| 1 |

+-------------------+

1 row in set (0.00 sec)

 

mysql> SELECT 123 IN(120,23,12);

+-------------------+

| 123 IN(120,23,12) |

+-------------------+

| 0 |

+-------------------+

 

---

 

IS [NOT] NULL,判断是空么

 

mysql> SELECT 0 IS NULL;

+-----------+

| 0 IS NULL |

+-----------+

| 0 |

+-----------+

 

常用在检查是否为空

 

mysql> SELECT * FROM test WHERE username IS NULL;

Empty set (0.00 sec)

 

----日期时间函数

 

---

 

NOW(),返回当时的日期和时间

 

CURDATE(),CURTIME()

 

---

 

DATE_ADD(),当前日期的变化

 

mysql> SELECT DATE_ADD('2012-2-23', INTERVAL 234 DAY);

+-----------------------------------------+

| DATE_ADD('2012-2-23', INTERVAL 234 DAY) |

+-----------------------------------------+

| 2012-10-14 |

+-----------------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT DATE_ADD('2012-2-23', INTERVAL -234 DAY);

+------------------------------------------+

| DATE_ADD('2012-2-23', INTERVAL -234 DAY) |

+------------------------------------------+

| 2011-07-04 |

+------------------------------------------+

1 row in set (0.00 sec)

 

---

 

DATEDIFF(),得到两个日期间的差值

 

mysql> SELECT DATEDIFF('2304-2-2','1234-3-22');

+----------------------------------+

| DATEDIFF('2304-2-2','1234-3-22') |

+----------------------------------+

| 390760 |

+----------------------------------+

1 row in set (0.02 sec)

 

---

 

DATE_FORMAT(),日期的格式化,日期的格式是可以选的,也就是说日期的格式转换

 

mysql> SELECT DATE_FORMAT('2013-2-22','%m/%d/%y');

+-------------------------------------+

| DATE_FORMAT('2013-2-22','%m/%d/%y') |

+-------------------------------------+

| 02/22/13 |

+-------------------------------------+

1 row in set (0.00 sec)

 

----信息函数

 

USER(),VERSION(),DATEBASE(),CONNECTION_ID(),LAST_INSERT_ID()

 

----聚合函数

 

只有一个返回值是他们的特点

 

AVG(),COUNT(),MAX(),MIN(),SUM()

 

直接调用可能会有错误,一般是针对数据表的字段进行的操作

 

mysql> SELECT AVG(id) FROM test;

+---------+

| AVG(id) |

+---------+

| 1.5000 |

+---------+

 

----加密函数

 

MD5(),PASSWORD()

 

mysql> SELECT MD5('HELLOWORLD');

+----------------------------------+

| MD5('HELLOWORLD') |

+----------------------------------+

| e81e26d88d62aba9ab55b632f25f117d |

+----------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT PASSWORD('HELLOWORLD');

+-------------------------------------------+

| PASSWORD('HELLOWORLD') |

+-------------------------------------------+

| *3456E7782A7F539BC823C715DB60231B0C7DE847 |

+-------------------------------------------+

1 row in set (0.00 sec)

 

一般而言,网页的编程用的都是MD5,password仅仅用于修改当前用户的密码

 

----

 

注重自带函数的熟悉、了解,灵活的调用和嵌套运用

Statement
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools