Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Essential difference between left join and right join (illustration)

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1, test1 table

2Test2 table

3, query

1 join left (1 table is a condition)

SELECT t1.id id1, t1.id1 A1, t1.id2 b1, t2.id1 a2, t2.id2 b2, t2.id id2FROM test1 T1 LEFT JOIN test2 T2 ON t1.id1 = t2.id2 WHERE t1.id1 = 2

Results:

2 join left (2 tables are conditions)

SELECT t1.id id1, t1.id1 A1, t1.id2 b1, t2.id id2, t2.id1 a2, t2.id2 b2 FROM test1 T1 LEFT JOIN test2 T2 ON t1.id1 = t2.id2 WHERE t2.id1 = 2

Results:

Summary: whether the table on the left or the table on the right mainly shows the table on the left, the table on the left can be repeated.

3The left join (there is a condition on the left and there is no condition on the right)

SELECT t1.id id1, t1.id1 A1, t1.id2 b1, t2.id id2, t2.id1 a2, t2.id2 b2 FROM test1 T1 LEFT JOIN test2 T2 ON t1.id1 = t2.id2 WHERE t1.id1 = 6

Results:

3The left join (there is a condition on the right and there is no condition on the left)

SELECT t1.id id1, t1.id1 A1, t1.id2 b1, t2.id id2, t2.id1 a2, t2.id2 b2 FROM test1 T1 LEFT JOIN test2 T2 ON t1.id1 = t2.id2 WHERE t2.id1 = 5

Results:

4 join left (query all fields)

SELECT t1.id id1, t1.id1 A1, t1.id2 b1, t2.id id2, t2.id1 a2, t2.id2 b2 FROM test1 T1 LEFT JOIN test2 T2 ON t1.id1 = t2.id2

Results:

5Query right join (query all fields)

SELECT t1.id id1, t1.id1 A1, t1.id2 b1, t2.id id2, t2.id1 a2, t2.id2 b2 FROM test1 T1 RIGHT JOIN test2 T2 ON t1.id1 = t2.id2

Results:

Summary:

The left connection and the right connection are just the opposite.

1. To ensure the integrity of the data, as long as it corresponds to the table condition on the left, it will be displayed.

2, if it does not correspond to the table on the left, it will not be displayed.

3, there is something on the left that shows the left, and if it doesn't exist on the right, it doesn't show.

[it is easy for yourself to understand and it is best to type it manually. Memory is resonated by multiple organs and the memory is deeper.]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report