一、基本的SELECT语句
1. “*”的注意事项:在SELECT语句中,用*来选取所有的列,这是一个应该抵制的习惯。
虽然节省了输入列名的时间,但是也意味着获得的数据比真正需要的数据多的多。相应的,也会降低应用程序的性能及网络性能。
良好的规则是只选所需。
2. join子句
jion是用来定义如何从多个表中选取数据并组合成一个结果集。
jion必需是因为(1)我们所要获取的所有信息并不都在一个表中,或者(2)所要返回的信息都在一个表中,但是其上设置的条件信息却在另一个表中。
jion的共同点是通过记录的连接列,把一条记录同一条或者多条其他记录进行匹配,从而产生出是这些记录的超级的记录。
2.1 INNER JOIN
INNER JOIN只返回进行联接的字段上匹配的记录。INNER JOIN是在做排除。
自引用是为了某种原因把一个表联接回它自身。
INNER JOIN是默认的联接方式。
2.2 OUTER JOIN
OUTER JOIN有左右联接之分,而INNER JOIN没有左右联接之分。
RIGHT(LEFT) OUTER JOIN是既想要包含右侧表中的所有行,以及左侧表中有匹配记录的行。
2.3 FULL JOIN
FULL JOIN联接,就是要包含位于联接两侧的表中所有的行。
2.4 CROSS JOIN
CROSS JOIN没有ON联接符,并且将join一侧表中的每一条记录与另一侧的表中所有的记录联接起来。即联接表中的笛卡尔积。
CROSS JOIN可用于提供样本数据和科学数据
3. WHERE子句
一些常用且不太熟悉的操作符:
BETWEEN: BETWEEN num1 AND num2
LIKE: LIKE "ANY%" %代表0个或者多个任意字符。_代表单个任意字符。[]表示括号中包含的任意单个字符。^排除下一个。
EXISTS: EXISTS 查询语句。
4. ORDER BY
你知道吗?查询的返回结果通常是以字母或者数字顺序方式给出,这是偶然的。
以何种方式给出,在没有指定的情况下,通常取决于SQLServer认为哪一种汇集数据的方式开销最小。因此,返回的结果通常是基于表中数据的物理顺序或者SQLServer用来找寻数据所使用的某个索引。
默认降序DESC,升序是ASC
ORDER BY 子句可以基于查询中使用的任何表中的任何字段来进行排序,无论该列是否包含在SELECT列表中。
5. 使用GROUP BY 子句聚集数据
一旦在查询语句中使用了GROUP BY,SELECT列表中的每一列要么包含在GROUP BY列表中,要不包含在聚集中。
当聚集不与GROUP BY一起使用时,聚集只能与其他聚集一起位于SELECT列表中,而不能与列名搭配出现在SELECT列表中。
除了COUNT(*) 之外,任何聚集函数都会忽略NULL值。
6. HAVING子句
仅当查询语句中有GROUP BY子句时使用HAVING子句。
WHERE子句应用到形成组的每一行上,HAVING子句应用到组的聚集上。
7. DISTINCT子句
DISTINCT消除重复数据。如果值是相同的,则该值出现一次。
DISTINCT出现在列表的开始处,或者出现在COUNT中。
二、基本的INSERT语句
1. 基本结构
INSERT [INTO] table_name [table_column_list] VALUES (data_value_list)
INTO是可以省略的。
table_column_list建议都显式给出,一是增强可读性,二是以后即使是表的结构发生改变,也不会有所影响。
2. INSERT INTO ... SELECT语句
批量插入数据。
INSERT [INTO] []
三、UPDATE语句更改现有数据
UPDATE
SET = [, = ]
[FROM ]
[WHERE ]
UPDATE 可以从一个表中生成数据,但是只影响一个表。
四、DELETE语句
DELETE
[FROM]
[FROM]
WHERE
有关DELETE的一个小例子:
films表 actors表
filmId | filmname | yearmade filmId | firstname | lastname
1 'host' 1984 1 'li' 'si'
2 'shit' 1999 2 'wang' 'wu'
3 'liu' 'li'
从actors表中删除在films表中没有匹配的行:
DELETE FROM actors
FROM actors a
LEFT JOIN films f on a.filmId = f.filmId
WHERE f.filmname is null;
MySQL 语法:delete a.* from actors a left join films f on a.yearmade = f.yearmade where f. yearmade is null
MySQL不支持双FROM。
五、UNION
UNION可以让两个或者更多个查询产生单个结果集。
JOIN水平的合并数据,而UNION垂直的合并数据。
UNION的几个要点:
1. 要进行UNION的SELECT 列表中列的数量是相通的。
2. 合并的结果集返回的标头仅取第一个查询。
3. 数据类型必须相同或者隐式兼容。
4. 返回默认是DISTINCT而非ALL。

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

The main reasons for poor MySQL query performance include not using indexes, wrong execution plan selection by the query optimizer, unreasonable table design, excessive data volume and lock competition. 1. No index causes slow querying, and adding indexes can significantly improve performance. 2. Use the EXPLAIN command to analyze the query plan and find out the optimizer error. 3. Reconstructing the table structure and optimizing JOIN conditions can improve table design problems. 4. When the data volume is large, partitioning and table division strategies are adopted. 5. In a high concurrency environment, optimizing transactions and locking strategies can reduce lock competition.

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.