Home Database Mysql Tutorial mysql left, right, inner join

mysql left, right, inner join

Jan 16, 2017 pm 01:06 PM

Left and right connections

Full multiplication method (very inefficient)

mysql> select * from test10;
+------+-------+
| id | sname |
+------+-------+
| 1 | 云彩 | 
| 2 | 月亮 | 
| 3 | 星星 | 
+------+-------+
Copy after login

3 rows in set (0.00 sec)

mysql> select * from test11;
+--------+-------+
| cat_id | cname |
+--------+-------+
| 95 | 猴子 | 
| 96 | 老虎 | 
+--------+-------+
Copy after login

2 rows in set (0.00 sec)

The effect of implementing two tables* in the database

mysql> select * from test10,test11;
+------+-------+--------+-------+
| id | sname | cat_id | cname |
+------+-------+--------+-------+
| 1 | 云彩 | 95 | 猴子 | 
| 1 | 云彩 | 96 | 老虎 | 
| 2 | 月亮 | 95 | 猴子 | 
| 2 | 月亮 | 96 | 老虎 | 
| 3 | 星星 | 95 | 猴子 | 
| 3 | 星星 | 96 | 老虎 | 
+------+-------+--------+-------+
Copy after login

6 rows in set (0.03 sec)

Analysis:
test10 Treated as a set with three elements
test11 Treated as a set with two elements

test10*test11The new set has six elements
Multiply the number of rows in two tables
Two columns Add the number of columns in each table (can be repeated)

When querying multiple tables with duplicate column names, you need to clearly indicate which table is obtained

mysql> select goods_id,minigoods.cat_id,goods_name,category.cat_id,cat_name from minigoods,category limit 3;
+----------+--------+--------------------+--------+----------+
| goods_id | cat_id | goods_name | cat_id | cat_name |
+----------+--------+--------------------+--------+----------+
| 1 | 4 | KD876 | 1 | 手机类型 | 
| 4 | 8 | htcN85原装充电器 | 1 | 手机类型 | 
| 3 | 8 | 诺基亚原装5800耳机 | 1 | 手机类型 | 
+----------+--------+--------------------+--------+----------+
Copy after login

Technique: Create a same structure Table create table [new table name] like [old table name]
create table minigoods like goods;

Copy part of the table contents

mysql> insert into minigoods
-> select * from goods limit 3;
Copy after login

Get meaningful two The corresponding minigoods.cat_id=category.cat_id

mysql> select goods_id,minigoods.cat_id,goods_name,category.cat_id,cat_name from minigoods,category where minigoods.cat_id=category.cat_id ;
+----------+--------+--------------------+--------+----------+
| goods_id | cat_id | goods_name | cat_id | cat_name |
+----------+--------+--------------------+--------+----------+
| 1 | 4 | KD876 | 4 | 3G手机 | 
| 4 | 8 | htcN85原装充电器 | 8 | 耳机 | 
| 3 | 8 | 诺基亚原装5800耳机 | 8 | 耳机 | 
+----------+--------+--------------------+--------+----------+
Copy after login

3 rows in set (0.00 sec)

Left join syntax
a table is on the left, not moving
B table is on the right, move
A table and B table pass a relationship (set by yourself) to filter the rows of b required by a

a left join b on [condition] --- -If the condition is true, take out the row of b

a left join b on [Condition] The result set can also be regarded as a table (assumed to be table c), and you can query it again

mysql> select goods_id,goods_name,cat_name 
-> from
-> (minigoods left join category on minigoods.cat_id=category.cat_id); 
+----------+--------------------+----------+
| goods_id | goods_name | cat_name |
+----------+--------------------+----------+
| 1 | KD876 | 3G手机 | 
| 4 | htcN85原装充电器 | 耳机 | 
| 3 | 诺基亚原装5800耳机 | 耳机 | 
+----------+--------------------+----------+
Copy after login

[minigoods left join category on minigoods.cat_id=category.cat_id regarded as c table]
Verification: You can still use where and other filtering conditions later

mysql> select goods_id,goods_name,cat_name from 
(minigoods left join category on minigoods.cat_id=category.cat_id )
where 1 order by goods_id desc limit 2; 
+----------+--------------------+----------+
| goods_id | goods_name | cat_name |
+----------+--------------------+----------+
| 4 | htcN85原装充电器 | 耳机 | 
| 3 | 诺基亚原装5800耳机 | 耳机 | 
+----------+--------------------+----------+
Copy after login

2 rows in set (0.00 sec)

You can join multiple tables left, that is, treat the result as a table
Treat it as one table

{{a left join b on [条件]} left join c on [条件]} 
mysql> select goods.goods_id,goods.goods_name,goods.cat_id,cat_name
-> from
-> minigoods left join category on minigoods.cat_id=category.cat_id
-> left join goods on minigoods.cat_id=4 limit 4;
+----------+--------------------+--------+----------+
| goods_id | goods_name | cat_id | cat_name |
+----------+--------------------+--------+----------+
| 1 | KD876 | 4 | 3G手机 | 
| 4 | htcN85原装充电器 | 8 | 3G手机 | 
| 3 | 诺基亚原装5800耳机 | 8 | 3G手机 | 
| 5 | 索爱原装M2卡读卡器 | 11 | 3G手机 | 
+----------+--------------------+--------+----------+
Copy after login

The difference between left and right joins ================================================== =================================================

a left join b on means query based on a when querying
a right join b on means query based on b when querying

a left join b on is equivalent For b right join a (all queries are based on a)

Tips: In terms of porting compatibility and understanding, it is best to always use left joins

create table boy(
bname varchar(20),
other char(1)
)engine myisam charset utf8;
insert into boy
values
('屌丝','A'),
('李四','B'),
('王五','C'),
('高富帅','D'),
('郑七','E');
Copy after login
create table girl(
gname varchar(20),
other char(1)
)engine myisam charset utf8;
insert into girl
values
('空姐','B'),
('大S','C'),
('阿娇','D'),
('张柏芝','D'),
('林黛玉','E'),
('宝钗','F');
Copy after login

Get the spouses of all boys (left connection)

select boy.*,girl.* from
boy left join girl on
boy.other=girl.other;
+--------+-------+--------+-------+
| bname | other | gname | other |
+--------+-------+--------+-------+
| 屌丝 | A | NULL | NULL | 
| 李四 | B | 空姐 | B | 
| 王五 | C | 大S | C | 
| 高富帅 | D | 阿娇 | D | 
| 高富帅 | D | 张柏芝 | D | 
| 郑七 | E | 林黛玉 | E | 
+--------+-------+--------+-------+
Copy after login

Get the spouses of all girls (left connection)

mysql> select girl.*,boy.* from
-> girl left join boy on 
-> boy.other=girl.other;
+--------+-------+--------+-------+
| gname | other | bname | other |
+--------+-------+--------+-------+
| 空姐 | B | 李四 | B | 
| 大S | C | 王五 | C | 
| 阿娇 | D | 高富帅 | D | 
| 张柏芝 | D | 高富帅 | D | 
| 林黛玉 | E | 郑七 | E | 
| 宝钗 | F | NULL | NULL | 
+--------+-------+--------+-------+
Copy after login

Get the spouses of all girls (right connection, and above Left join is consistent)

mysql> select girl.* ,boy.* from
-> boy right join girl on
-> boy.other=girl.other;
+--------+-------+--------+-------+
| gname | other | bname | other |
+--------+-------+--------+-------+
| 空姐 | B | 李四 | B | 
| 大S | C | 王五 | C | 
| 阿娇 | D | 高富帅 | D | 
| 张柏芝 | D | 高富帅 | D | 
| 林黛玉 | E | 郑七 | E | 
| 宝钗 | F | NULL | NULL | 
+--------+-------+--------+-------+
Copy after login

Inner join==================================== ================================================== ===============

Take out the spouse

select girl.*,boy.* from
boy inner join girl on 
boy.other=girl.other;
+--------+-------+--------+-------+
| gname | other | bname | other |
+--------+-------+--------+-------+
| 空姐 | B | 李四 | B | 
| 大S | C | 王五 | C | 
| 阿娇 | D | 高富帅 | D | 
| 张柏芝 | D | 高富帅 | D | 
| 林黛玉 | E | 郑七 | E | 
+--------+-------+--------+-------+
Copy after login

The inner join is the intersection of the left and right joins
(outer The connection is the union of left and right connections, which is not supported by mysql) You can use union to implement

Left join application========================== ================================================== =========================

create table match_t(
match_id int primary key auto_increment,
host_team_id int,
guest_team_id int,
match_result varchar(20),
match_time date
)engine myisam charset utf8;
insert into match_t
values
(1,1,2,'2:0','2006-05-21'),
(2,2,3,'1:2','2006-06-21'),
(3,3,1,'2:5','2006-07-21'),
(4,1,1,'3:2','2006-08-21');
create table team_t(
team_id int primary key auto_increment,
team_name varchar(20)
)engine myisam charset utf8;
insert into team_t
values
(1,'恒大'),
(2,'国安'),
(3,'申花'),
(4,'大连');
mysql> select * from match_t;
+----------+--------------+---------------+--------------+------------+
| match_id | host_team_id | guest_team_id | match_result | match_time |
+----------+--------------+---------------+--------------+------------+
| 1 | 1 | 2 | 2:0 | 2006-05-21 | 
| 2 | 2 | 3 | 1:2 | 2006-06-21 | 
| 3 | 3 | 1 | 2:5 | 2006-07-21 | 
| 4 | 4 | 1 | 3:2 | 2006-08-21 | 
+----------+--------------+---------------+--------------+------------+
mysql> select * from team_t;
+---------+-----------+
| team_id | team_name |
+---------+-----------+
| 1 | 恒大 | 
| 2 | 国安 | 
| 3 | 申花 | 
| 4 | 大连 | 
+---------+-----------+
Copy after login

Skills: Kindness and giving show its alias

2006 - After the date of 06-21, change the battle in the table to the team name

select host_t.team_name,guest_t.team_name,match_result,match_time from
match_t left join (team_t as host_t) on match_t.host_team_id=host_t.team_id
left join (team_t as guest_t) on match_t.guest_team_id=guest_t.team_id
where match_time>='2006-06-21';
+--------------+-----------+---------------+-----------+--------------+------------+
| host_team_id | team_name | guest_team_id | team_name | match_result | match_time |
+--------------+-----------+---------------+-----------+--------------+------------+
| 1 | 恒大 | 2 | 国安 | 2:0 | 2006-05-21 | 
| 2 | 国安 | 3 | 申花 | 1:2 | 2006-06-21 | 
| 3 | 申花 | 1 | 恒大 | 2:5 | 2006-07-21 | 
| 4 | 大连 | 1 | 恒大 | 3:2 | 2006-08-21 | 
+--------------+-----------+---------------+-----------+--------------+------------+
Copy after login

The above is the content of mysql left, right, and inner connections. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!


Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

See all articles