博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL join
阅读量:4589 次
发布时间:2019-06-09

本文共 4076 字,大约阅读时间需要 13 分钟。

SQL left join, right join

 

http://stackoverflow.com/questions/5706437/whats-the-difference-between-inner-join-left-join-right-join-and-full-join

An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.

There are different types of joins available in SQL:

INNER JOIN: returns rows when there is a match in both tables.

LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table.

RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table.

FULL JOIN: returns rows when there is a match in one of the tables.

SELF JOIN: is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.

CARTESIAN JOIN: returns the Cartesian product of the sets of records from the two or more joined tables.

WE can take each first four joins in Details :

We have two tables with the following values.

TableA

id  firstName                  lastName.......................................1 arun prasanth 2 ann antony 3 sruthy abc 6 new abc

TableB

id2 age Place................1 24 kerala 2 24 usa 3 25 ekm 5 24 chennai

....................................................................

INNER JOIN

Note :it gives the intersection of the two tables, i.e. rows they have common in TableA and TableB

Syntax

SELECT table1.column1, table2.column2... FROM table1 INNER JOIN table2 ON table1.common_field = table2.common_field;

Apply it in our sample table :

SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place FROM TableA INNER JOIN TableB ON TableA.id = TableB.id2;

Result Will Be

firstName       lastName       age  Place..............................................arun            prasanth        24 kerala ann antony 24 usa sruthy abc 25 ekm

LEFT JOIN

Note : will give all selected rows in TableA, plus any common selected rows in TableB.

Syntax

SELECT table1.column1, table2.column2... FROM table1 LEFT JOIN table2 ON table1.common_field = table2.common_field;

Apply it in our sample table :

SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place FROM TableA LEFT JOIN TableB ON TableA.id = TableB.id2;

Result

firstName                   lastName                    age   Place...............................................................................arun                        prasanth                    24 kerala ann antony 24 usa sruthy abc 25 ekm new abc NULL NULL

RIGHT JOIN

Note : will give all selected rows in TableB, plus any common selected rows in TableA.

Syntax

SELECT table1.column1, table2.column2... FROM table1 RIGHT JOIN table2 ON table1.common_field = table2.common_field;

Apply it in our sample table :

SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place FROM TableA RIGHT JOIN TableB ON TableA.id = TableB.id2;

Result

firstName                   lastName                    age     Place...............................................................................arun                        prasanth                    24 kerala ann antony 24 usa sruthy abc 25 ekm NULL NULL 24 chennai

FULL JOIN

Note : It is same as union operation, it will return all selected values from both tables.

Syntax

SELECT table1.column1, table2.column2... FROM table1 FULL JOIN table2 ON table1.common_field = table2.common_field;

Apply it in our sample table :

SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place FROM TableA FULL JOIN TableB ON TableA.id = TableB.id2;

Result

firstName                   lastName                    age    Place...............................................................................arun                        prasanth                    24 kerala ann antony 24 usa sruthy abc 25 ekm new abc NULL NULL NULL NULL 24 chennai

Interesting Fact

For INNER joins the order doesn't matter

For (LEFT, RIGHT or FULL) OUTER joins,the order matter

Better to go check this  it will give you interesting details about join order

posted on
2016-03-23 16:55 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/dataclimber/p/5311862.html

你可能感兴趣的文章
框架设计读书笔记--扩展点设计--组合法
查看>>
Web开发小贴士 -- 全面了解Cookie
查看>>
收藏Javascript中常用的55个经典技巧
查看>>
Arm-linux-gcc-4.3.2安装步骤
查看>>
Java多线程与并发编程学习
查看>>
Support Vector Machine
查看>>
牛客-2018多校算法第五场C-KMP
查看>>
Linux查看文件内容
查看>>
[转]社会生活中十二大著名法则 1 马太效应 2 手表定理 3 不值得定律 4 彼得原理 5 零和游戏原理 6 华盛顿合作规律 7 酒与污水定律 8 水桶定律 9 蘑菇管理 10 奥...
查看>>
浅谈三层与实体
查看>>
cocostudio——js 3 final控件事件
查看>>
Flex 学习笔记 datatip的背景颜色
查看>>
iOS开发中六种手势识别
查看>>
oracle创建临时表没有权限
查看>>
76.数塔问题
查看>>
PHP 透明水印生成代码
查看>>
我就是学习
查看>>
Hibernate的缓存
查看>>
(十五)Struts2 本地化/国际化(i18n)
查看>>
利用Qt Phonon框架制作音视频播放器
查看>>