mysql左连接,右连接,内连接学习体会
声明:本文为本人在分析学习了其他网友的博文后,得出的一些体会。。编写本文的目的,一则,分享自己的学习体会,希望通过此文与其他正在学习数据库的网友互相交流,共同学习;二则,记录下学习中的点滴收获,供以后温习。本人为数据库初学者,有很多观点可
声明:本文为本人在分析学习了其他网友的博文后,得出的一些体会。。编写本文的目的,一则,分享自己的学习体会,希望通过此文与其他正在学习数据库的网友互相交流,共同学习;二则,记录下学习中的点滴收获,供以后温习。本人为数据库初学者,有很多观点可能存在错误,若诸位网友有不同观点,望不吝指教。
首先引用一下这位网友的博文,本人从此博文中受益良多,感谢此位网友的分享。
http://hi.baidu.com/woaidelphi/item/d96be15c3ce2c1cbd2e10c6c
author:华夏之星
QQ:1019822077
email:woai_php@sina.com
内连接: 只连接匹配的行
左外连接: 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行
右外连接: 包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边表中全部匹配的行
注:此博文中还讲述了其他连接方式,鉴于本人刚开始学习数据库,因此,仅研究较为普遍的3中方式。
内连接与左右连接
在网上看到很多讲述内连接和左右连接的博文,一直认为这是3类连接方式。但看完这篇博文后,认识到,其实这3中连接方式,实际可以分为
两类。一种内连接,一种外连接。而外连接又可分为左连接和右连接。
内连接的匹配方式
------------------------------------------------------------------------------再次引用-----------------------------------------------------------------------
举个例子吧。
表A
id name
1 张
2 李
3 王
表B
id address A_id
1 北京 1
2 上海 3
3 南京 10
/******************************** inner join 内连接*****************************************/
排他性:A,B表中至少有1个匹配时,才返回行。两表的交集
SQL语句如下:
select A.name,B.address from A
inner join B
on A.id = B.A_id
查询结果为:
name address
张 北京
王 上海
------------------------------------------------------------------------------引用结束-----------------------------------------------------------------------
此sql查询语句可按如下翻译:使用内连接的方式,选择出A中id与B中A_id相等的A表和B表中的行,然后将A表中行的name字段和B表中的address字段组合成行,作为查询结果。
具体过程可以描述为,对于A表中id为1的行(及表中第一行),与B表中的每一行都进行匹配,找到与B表中A_id字段相同的行(本例中B表的第一行便满足条件)。由于本例中,A表的第一行和B表的第一行便满足SQL语句的查询条件,因此,查询结果集的第一条结果为A表第一行的name(张)和B表第一行的address(北京)。然后,对A表的第二行进行匹配,遍历B表的所有行,没有找到满足与A表id(2)相同的A_id的行,因此。接着为A表的第3行,在B表中寻找匹配的行。遍历B表中所有的行,发现第二行的A_id(3)与A表id(3)匹配,因此,匹配成功,输出结果。自此,A表的所有行的查询了,本次SQL查询结束。结果有两条匹配的结果。
左右连接的匹配过程
--------------------------------------------------------------------------接着引用---------------------------------------------------------------
表A
id name
1 张
2 李
3 王
表B
id address A_id
1 北京 1
2 上海 3
3 南京 10
/********************************
left join 左连接*****************************************/
包容性:A表包容B表,左连接左表是全的.(left join 或 left outer join )
SQL语句如下:
SELECT A.name, B.address
FROM A
LEFT JOIN B ON A.id = B.A_id
查询结果为:
name address
张 北京
李 NULL
王 上海
/******************************** right join 右连接*****************************************/
包容性:B表包容A表,右连接右表是全的.(right join 或 right outer join )
SQL语句如下:
SELECT A.name, B.address
FROM A
RIGHT JOIN B ON A.id = B.A_id
查询结果为:
name address
张 北京
王 上海
NULL 南京
------------------------------------------------------------------------------引用结束-----------------------------------------------------------------------
左右连接的匹配过程与内连接的匹配过程相同,不同的是,左右连接查询时,SQL语句中,指定的基准表(right join 或left join 后的表)字段
无论是否在另一张表中有与之匹配的行,基准表中的字段均会输出。如上例左连接中的第二行,李(左连接基准表指定的查询字段)和右连接中的
第3行南京(右连接基准表中指定的查询字段)

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
