


Detailed explanation of examples of classification of SQL query statements in mysql
There are many kinds of SQL query statements, summarized below. First, build three tables for later experiments
-- 学生表,记录学生信息 CREATE TABLE student( sno VARCHAR(10), sname VARCHAR(10), ssex ENUM('男','女'), sage INT, sdept VARCHAR(10), PRIMARY KEY(sno) ); +-----------+-------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-----------+-------+------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | | 201215122 | 刘晨 | 女 | 19 | CS | | 201215123 | 王敏 | 女 | 18 | MA | | 201215125 | 张立 | 男 | 19 | IS | +-----------+-------+------+------+-------+ -- 课程表,记录课程信息,cpno是指当前记录的先行课程的cno CREATE TABLE course( cno INT AUTO_INCREMENT, cname VARCHAR(10), cpno INT, ccredit INT NOT NULL, PRIMARY KEY(cno), FOREIGN KEY(cpno) REFERENCES course(cno) ); +-----------+-------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-----------+-------+------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | | 201215122 | 刘晨 | 女 | 19 | CS | | 201215123 | 王敏 | 女 | 18 | MA | | 201215125 | 张立 | 男 | 19 | IS | +-----------+-------+------+------+-------+ -- 选课记录表,记录选课信息 CREATE TABLE sc ( sno VARCHAR(10), cno INT, grade INT ); +-----------+------+-------+ | sno | cno | grade | +-----------+------+-------+ | 201215121 | 1 | 92 | | 201215121 | 2 | 85 | | 201215121 | 3 | 88 | | 201215122 | 1 | 90 | | 201215122 | 2 | 80 | +-----------+------+-------+
1. Single-table query
A query statement that only involves one table is called a single-table query statement, as an example.
SELECT * FROM student; SELECT FROM student WHERE sage>=20;
These statements only involve one table, so they are single-table query statements.
2. Multi-table query
Corresponds to single-standard query. A query involving multiple tables is a multi-table query, which is divided into join query, nested query, derived table query and set. Inquire.
2.1 Connection query
Connection query is the most commonly used query statement in database queries. It refers to the connection through connection fields and connection conditions Multiple tables can be queried, and connection queries are divided into subcategories: equijoin, non-equivalent join, natural join, outer join, inner join, and self-join.
Equivalent connection and non-equivalent connection
When the connection condition is the equal sign (=), the connection is called an equivalence connection. On the contrary, when the connection condition is the equal sign (=) Either the equal sign or the non-equivalent connection.
-- 查询每个学生的选修课情况,连接条件是等于,连接字段是sno SELECT * FROM student,sc WHERE student.sno = sc.sno; +-----------+-------+------+------+-------+-----------+------+-------+ | sno | sname | ssex | sage | sdept | sno | cno | grade | +-----------+-------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 1 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 80 | +-----------+-------+------+------+-------+-----------+------+-------+
The process of the connection operation is to first take out the first record in the student table, and then match it with all the records in the sc table according to the connection conditions and connection fields, and then connect them directly to form the result A tuple in the table. Then match the second record of the student table with the sc table, the third record..., and repeat until the fetching is completed. This matching algorithm is called Nested Loop Join Algorithm
Inner Join
Inner join is another way of writing equal join or non-equivalent join. The writing method is: There are two types of INNER JOIN ON or CORSS JOIN USING
-- 使用内连接查询每个学生的选修课情况,查询结果和使用上面的等值连接一样。 -- 在MySQL中,INNER可省略,CROSS JOIN= INNER JOIN = INNER SELECT * FROM student INNER JOIN sc ON student.sno=sc.sno; SELECT * FROM student JOIN sc ON student.sno=sc.sno; SELECT * FROM student CROSS JOIN sc USING(sno); +-----------+-------+------+------+-------+-----------+------+-------+ | sno | sname | ssex | sage | sdept | sno | cno | grade | +-----------+-------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 1 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 80 | +-----------+-------+------+------+-------+-----------+------+-------+
Outer joins (left outer join, right outer join, full outer join)
The existence of outer joins can make up for inner joinsOnly match tuples that meet the conditionsThe defect, that is to say, the inner join can only query the tuples that meet the join conditions in the two tables, and the outer join can make up for this to some extent. defect. Outer joins are divided into Left outer join (based on the table on the left side of the JOIN keyword, and if there is no matching record, NULL is set), Right outer join (based on the table on the right side of the JOIN keyword) (based on the tables on the left and right sides of the JOIN keyword), Full outer join (based on the tables on the left and right sides of the JOIN keyword). MySQL does not support full outer joins, but it can be done using set queries, that is, UNION ALL operations are performed on the query results of the left outer join and the query results of the right outer join.
-- 左外连接,以左边的表student为基准。 在MySQL中,OUTER关键字在MySQL中可省略 LEFT JOIN=LEFT OUTER JOIN,RIGHT JOIN=RIGHT OUTER JOIN SELECT * FROM student LEFT OUTER JOIN sc ON student.sno=sc.sno; SELECT * FROM student LEFT JOIN sc ON student.sno=sc.sno; +-----------+-------+------+------+-------+-----------+------+-------+ | sno | sname | ssex | sage | sdept | sno | cno | grade | +-----------+-------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 1 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 80 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | NULL | +-----------+-------+------+------+-------+-----------+------+-------+ -- 右外连接,注意sc和student换了位置 SELECT * FROM sc RIGHT OUTER JOIN student ON student.sno=sc.sno; +-----------+------+-------+-----------+-------+------+------+-------+ | sno | cno | grade | sno | sname | ssex | sage | sdept | +-----------+------+-------+-----------+-------+------+------+-------+ | 201215121 | 1 | 92 | 201215121 | 李勇 | 男 | 20 | CS | | 201215121 | 2 | 85 | 201215121 | 李勇 | 男 | 20 | CS | | 201215121 | 3 | 88 | 201215121 | 李勇 | 男 | 20 | CS | | 201215122 | 1 | 90 | 201215122 | 刘晨 | 女 | 19 | CS | | 201215122 | 2 | 80 | 201215122 | 刘晨 | 女 | 19 | CS | | NULL | NULL | NULL | 201215123 | 王敏 | 女 | 18 | MA | | NULL | NULL | NULL | 201215125 | 张立 | 男 | 19 | IS | +-----------+------+-------+-----------+-------+------+------+-------+ -- 全外连接 SELECT * FROM sc FULL JOIN student ON student.sno=sc.sno; ERROR 1054 (42S22): Unknown column 'sc.sno' in 'on clause' -- 注意是UNION ALL,而非UNION,UNION有个去重效果 SELECT * FROM student LEFT OUTER JOIN sc ON student.sno=sc.sno UNION ALL SELECT * FROM student RIGHT OUTER JOIN sc ON student.sno=sc.sno; +-----------+-------+------+------+-------+-----------+------+-------+ | sno | sname | ssex | sage | sdept | sno | cno | grade | +-----------+-------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 1 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 80 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | NULL | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 1 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 80 | +-----------+-------+------+------+-------+-----------+------+-------+
Natural connection (all natural connection, left natural connection, right natural connection)
Removing the same attributes in the equivalent connection is a natural connection or all natural connection, Left natural joinMatching based on the left table, Right natural joinMatching based on the right table
-- 查询每个学生的选修课情况,自然连接,去除相同的属性sno SELECT student.sno,student.sname,student.ssex,student.sage,student.sdept,sc.cno,sc.grade FROM student,sc WHERE student.sno = sc.sno; SELECT * FROM student NATURAL JOIN sc; +-----------+-------+------+------+-------+------+-------+ | sno | sname | ssex | sage | sdept | cno | grade | +-----------+-------+------+------+-------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | 1 | 92 | | 201215121 | 李勇 | 男 | 20 | CS | 2 | 85 | | 201215121 | 李勇 | 男 | 20 | CS | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 1 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 2 | 80 | +-----------+-------+------+------+-------+------+-------+ SELECT * FROM student NATURAL LEFT JOIN sc; +-----------+-------+------+------+-------+------+-------+ | sno | sname | ssex | sage | sdept | cno | grade | +-----------+-------+------+------+-------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | 1 | 92 | | 201215121 | 李勇 | 男 | 20 | CS | 2 | 85 | | 201215121 | 李勇 | 男 | 20 | CS | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 1 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 2 | 80 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | +-----------+-------+------+------+-------+------+-------+ -- sc和student位置交换了,仍已student为基准,以为王敏、张立没有选课,所以有NULL字段 SELECT * FROM sc NATURAL RIGHT JOIN student; +-----------+-------+------+------+-------+------+-------+ | sno | sname | ssex | sage | sdept | cno | grade | +-----------+-------+------+------+-------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | 1 | 92 | | 201215121 | 李勇 | 男 | 20 | CS | 2 | 85 | | 201215121 | 李勇 | 男 | 20 | CS | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 1 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 2 | 80 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | +-----------+-------+------+------+-------+------+-------+
Self-join
As the name suggests, self-connection is a table, connecting itself to itself.
-- '数据库'的先修课信息,连接条件是course1.cno = course2.cpno SELECT * FROM course AS course1,course AS course2 WHERE course1.cno = course2.cpno AND course1.cno = 4 +-----+--------+------+---------+-----+--------+------+---------+ | cno | cname | cpno | ccredit | cno | cname | cpno | ccredit | +-----+--------+------+---------+-----+--------+------+---------+ | 4 | 数据库 | 2 | 4 | 7 | PASCAL | 4 | 4 | +-----+--------+------+---------+-----+--------+------+---------+
2.2 Nested query
First introduce the concept of a query block, a SQL statement in the form of SELECT...FROM...WHERE... is called query block. When the SELECT clause or WHERE clause of a query block is nested in the query statement of another query block, it is called a nested query. The outermost query is called outer query or parent query, and the innermost query is called inner query or subquery. When a subquery uses the data (tables, fields) of the parent query, it is called a correlated subquery. On the contrary, if it is not used, it is called an irrelevant subquery. Nested queries are usually used in conjunction with IN, ALL, ANY, and EXISTS.
-- 查询与刘晨在同一个系中的学生(先查出刘晨所在系,再查该系中的学生) -- 内层查询可以独立运行没有依赖于外层,所以是不相关子查询 SELECT * FROM student WHERE sdept IN ( SELECT sdept FROM student WHERE sname='刘晨' ) +-----------+-------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-----------+-------+------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | | 201215122 | 刘晨 | 女 | 19 | CS | +-----------+-------+------+------+-------+ -- 查询选修了‘信息系统’的学生信息(先查出信息系统的课程号cno,再查处所有选课信息,再查出学生信息) -- 同样,也是不相关子查询 SELECT * FROM student WHERE sno IN ( SELECT sno FROM sc WHERE cno IN ( SELECT cno FROM course WHERE cname='信息系统' ) ) +-----------+-------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-----------+-------+------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | +-----------+-------+------+------+-------+ -- 找出每个学生超过自己选修课平均成绩的选课信息(先查出平均成绩,再查出选课信息) -- 内层查询无法独立运行,所以是相关子查询 SELECT * FROM sc AS x WHERE grade >= ( SELECT AVG(grade) FROM sc AS y WHERE x.sno AND y.sno ) +-----------+------+-------+ | sno | cno | grade | +-----------+------+-------+ | 201215121 | 1 | 92 | | 201215121 | 3 | 88 | | 201215122 | 1 | 90 | +-----------+------+-------+
2.3 Derived table query
Personally think it is also a kind of nested query, but it is widely used, so I proposed it. When the query block appears after the FROM clause, it is called a derived table query.
-- 查询所有选修了cno=1的课程的学生信息 SELECT * FROM student,( SELECT sno FROM SC WHERE cno=1 ) AS tempSC WHERE student.sno = tempSC.sno +-----------+-------+------+------+-------+-----------+ | sno | sname | ssex | sage | sdept | sno | +-----------+-------+------+------+-------+-----------+ | 201215121 | 李勇 | 男 | 20 | CS | 201215121 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | +-----------+-------+------+------+-------+-----------+
2.4 Set Query
Query operations involving UNION, UNION ALL, INTERSECT, and EXCEPT are called set queries. Among them, UNION and UNION ALL will perform union, but UNION will remove duplicate records. Finally, MySQL does not support INTERSECT and EXCEPT.
--查询CS系及年龄不大于19岁的学生(CS系的学生与年龄不大于19岁的学生做并集) SELECT * FROM student WHERE sdept='CS' UNION ALL SELECT * FROM student WHERE sage<=19 +-----------+-------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-----------+-------+------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | | 201215122 | 刘晨 | 女 | 19 | CS | | 201215122 | 刘晨 | 女 | 19 | CS | | 201215123 | 王敏 | 女 | 18 | MA | | 201215125 | 张立 | 男 | 19 | IS | +-----------+-------+------+------+-------+ -- UNION去重 SELECT * FROM student WHERE sdept='CS' UNION SELECT * FROM student WHERE sage<=19 +-----------+-------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-----------+-------+------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | | 201215122 | 刘晨 | 女 | 19 | CS | | 201215123 | 王敏 | 女 | 18 | MA | | 201215125 | 张立 | 男 | 19 | IS | +-----------+-------+------+------+-------+ -- 查询计算机系年龄不大于19岁的学,计算机系的学生与年龄不大于19岁的学生取交集,MySQL不支持INTERSECT操作 SELECT * FROM student WHERE sdept='cs' INTERSECT SELECT * FROM student WHERE sage<=19 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTERSECT SELECT * FROM student WHERE sage<=19' at line 2 -- 用内连接代替 SELECT a.* FROM student AS a INNER JOIN student AS b ON a.sno=b.sno WHERE a.sdept='CS' AND b.sage<=19 +-----------+-------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-----------+-------+------+------+-------+ | 201215122 | 刘晨 | 女 | 19 | CS | +-----------+-------+------+------+-------+ -- 查询计算机系中年龄大于19岁的学生,就是查询计算机系的学生与年龄不大于19岁的学生的差集,MySQL不支持EXCEPT操纵 SELECT * FROM student WHERE sdept='CS' EXCEPT SELECT * FROM student WHERE sage<=19 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EXCEPT SELECT * FROM student WHERE sage<=19' at line 2 -- 用外连接或普通连接代替 SELECT a.* FROM student AS a LEFT JOIN student AS b ON a.sno=b.sno WHERE a.sdept='CS' AND b.sage>19 AND b.sno IS NOT NULL SELECT * FROM student WHERE sdept='CS' AND sage>19; +-----------+-------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-----------+-------+------+------+-------+ | 201215121 | 李勇 | 男 | 20 | CS | +-----------+-------+------+------+-------+
Summary
The above is the detailed content of Detailed explanation of examples of classification of SQL query statements in mysql. For more information, please follow other related articles on the PHP Chinese website!

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 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 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.

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.

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

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.


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

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.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor