search
HomeDatabaseMysql Tutorial外链接操作小结 inner join & left join & right join_MySQL

    数据库操作语句

7. 外连接——交叉查询
7.1 查询
7.2 等值连接
7.3 右外连接
7.4 左外连接
7.5 更新操作

简介:

外部连接和自联接
inner join(等值连接) 只返回两个表中联结字段相等的行
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
on 指定表间联结字段及其关系的等号 "=" 表达式, 返回 true 或 false. 当表达式返回 true 时, 则查询中包含该记录.
! 外部连接只能操作已存在于数据库中的数据

CODE:

7.5.2
UPDATE (ctarticle AS a LEFT JOIN ctclass AS c ON a.classid = c.classid) LEFT JOIN cttag AS b ON a.articleid = b.articleid
SET tag=tag+' ', b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid
WHERE a.classid=23 AND a.nclassid=0 AND tagid is not null

7.5.1
UPDATE (ctarticle AS a LEFT JOIN (ctnclass AS c LEFT JOIN ctclass AS d ON c.classid = d.classid) ON a.nclassid = c.nclassid AND a.classid = c.classid) LEFT JOIN cttag AS b ON a.articleid = b.articleid SET tag=d.class+' '+c.nclass, b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid
WHERE a.classid=23 AND a.nclassid=197;

7.5 更新操作

74.5 左连接中数据的筛选
INSERT INTO cttag(articleid,classid,nclassid) SELECT a.articleid,a.classid,a.nclassid from ctarticle a left join cttag b on a.articleid=b.articleid where b.articleid is null
//本语句功能为, 显示主表的全部内容, 插入数据到副表中没有的数据
//主要作用为: 让数据减少冗余

7.4.4.1 上例中的延续
SELECT a.*, b.*, c.*, d.*
FROM cttag as d left join ((ctarticle AS a LEFT JOIN ctclass AS b ON a.classid=b.classid) LEFT JOIN ctnclass AS c ON a.nclassid=c.nclassid) on d.articleid=a.articleid;

7.4.4 显示文章表中的全部, 调用类别表中的栏目
select a.*, b.*, c.* from (ctarticle a left join ctclass b on a.classid=b.classid) left join ctnclass c on a.nclassid=c.nclassid
//作用, 有时在文章表中包含了在个别类别表中没有的数据, 用这个语法可以读出文章表的全部数据
//a 为 文章表, b 为主类别, c 为子类别

7.4.3 同上例, 选择追加数据时加上空格
INSERT INTO cttag(articleid,classid,nclassid,tag)
SELECT a.articleid,a.classid,a.nclassid,d.class+' '+c.nclass
FROM (ctarticle AS a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=4 and a.nclassid=154;

7.4.2 连接N个表, 并追加数据到其中一个表, N=4
INSERT INTO cttag(articleid,classid,nclassid,tag)
SELECT a.articleid,a.classid,a.nclassid,d.class+c.nclass
FROM (ctarticle AS a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=1 and a.nclassid=1;
//解读
插入到 表2(栏1,栏2,栏3,栏4)
选择 别名a.栏1, 别名a.栏2, 别名a.栏3, 别名d.栏4 加上 别名c.栏5
从 (表1 别名a 左连接 (表3 别名c 左连接 表4 别名d 在 别名c.栏2 等于 别名d.栏2) 在 别名a.栏2 等于 别名c.栏2 和 别名a.栏3=别名c.栏3) 左连接 表2 别名b 在 别名a.栏1 等于 别名b.栏1 在那里 别名a.栏2=1 和 别名a.栏3=1

7.4.1 连接两个表, 并追加数据到其中一个表
INSERT INTO cttag(articleid,classid,nclassid)
SELECT a.articleid,a.classid,a.nclassid
FROM ctarticle AS a LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=1 and a.nclassid=1;
//解读
插入到 表2(栏1,栏2,栏3)
选择 别名a.栏1, 别名a.栏2, 别名a.栏3
从 表1 别名a 左连接 表2 别名b 在 别名a.栏1 等于 别名b.栏1 在那里 别名a.栏4=1 和 别名a.栏5=1

7.4. 左连接

7.3.1 同步两表的数据
UPDATE ctarticle a INNER JOIN cttag b ON a.articleid = b.articleid SET b.classid=a.classid, b.nclassid=a.nclassid;
//解读
更新 表1 别名a 联接 表2 别名2 在 别名a.栏1 等于 别名b.栏1 设置 别名b.栏2 更新为 别名a.栏2, 别名b.栏3 更新为 别名a.栏3

7.3 右外连接
select a.*, b.* from bunclass a right join ctclass b on a.classid=b.classid where a.nclassid=20
查询别名 a,b 表, 只匹配 b 表中的内容.

7.2.3 添加数据到连接表之一
INSERT INTO cttag ( tag, articleid ) SELECT top 1 b.tag, a.articleid FROM ctarticle AS a left JOIN cttag AS b ON a.articleid = b.articleid WHERE a.articleid order by a.articleid desc;

7.2.2 变通中的用法二
INSERT INTO bureply
SELECT b.*, a.classid, a.nclassid
FROM article AS a INNER JOIN reply AS b ON a.articleid = b.articleid
WHERE classid=50;

7.2.1 实际应用中的变通
INSERT INTO butag ( tag, articleid, classid, nclassid)
SELECT b.tag, a.articleid, a.classid, a.nclassid
FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
WHERE classid=24;

7.2 添加数据到其他表
INSERT INTO butag ( tag, articleid )
SELECT b.tag, a.articleid
FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
WHERE a.articleidFalse;
//解读
添加到 接收表(列1,列2)
选择 别名b.列1, 别名a.列2
从 表1 表名a 联接 表2 表名b 在 别名a.列c 等于 别名b.列c
在哪里 别名a.列c 不等于 没有

7.1.1 实际应用中的变通
SELECT b.tag, a.articleid, a.classid, a.nclassid
FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
WHERE a.classid=24;

7.1 查询
SELECT b.tag, a.articleid
FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
WHERE a.articleidFalse;
//解读
选择 别名b.列, 别名a.列
从 表1 别名a 联接 表2 别名b 在 别名a.列c = 别名b.列c
在哪里 别名a.列c 不等于 没有
注: as 不是必要

7. 外连接——交叉查询
提示: 注意表中相同的栏目

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

MySQL's Role: Databases in Web ApplicationsMySQL's Role: Databases in Web ApplicationsApr 17, 2025 am 12:23 AM

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.

MySQL: Building Your First DatabaseMySQL: Building Your First DatabaseApr 17, 2025 am 12:22 AM

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: A Beginner-Friendly Approach to Data StorageMySQL: A Beginner-Friendly Approach to Data StorageApr 17, 2025 am 12:21 AM

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.

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

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

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

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

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

Atom editor mac version download

The most popular open source editor