`
独善其身008
  • 浏览: 168566 次
  • 性别: Icon_minigender_2
  • 来自: 苏州
文章分类
社区版块
存档分类
最新评论

SQL多表关联! inner join,left join ,right join

 
阅读更多

SQL多表关联! inner join,left join ,right join
2007-08-06 20:35

我对多表关联的总结://以下若有理解错误请指出,谢谢!
内连接      
INNER JOIN:只显示多表之间与关联条件相匹配的列.

外连接:
LEFT JOIN :以左表为基础,显示左表中的所有列,不管是否与关联条件相匹配,而右表中的数据只显示与关联条件相匹配的列,不匹配的列以NULL字符填充.

RIGHT JOIN:以右表为基础,显示右表中的所有列,不管是否与关联条件相匹配,而左表中的数据只显示与关联条件相匹配的列,不匹配的列以NULL字符填充.

FULL JOIN :显示多个表中的所有的列,不匹配关联条件的列以NULL字符填充.

下面是我写的关于sql 多表关联的sql语句,大家可以在SQL SERVER中操作下试下,是否与上面的对于sql关联的总结相同,查询结果我就不结了,大家试下就知道了!

create database mydb;

use mydb;

create table customers
(
cust_id int not null identity(1,1) primary key,
cust_name varchar(100) not null,
cust_tel varchar(50) not null,
cust_address varchar(100) not null 
)

create table books
(
isbn int not null identity(700001,1),
book_name varchar(100) not null,
price money not null
)

create table orders
(
order_id int not null identity(1001,1),
isbn int not null,
cust_id int not null,
order_date datetime not null
)


insert into customers(cust_name,cust_tel,cust_address) values('书店a','0553-2451466','江苏省');
insert into customers(cust_name,cust_tel,cust_address) values('书店b','0553-2215266','江苏省');
insert into customers(cust_name,cust_tel,cust_address) values('书店c','0553-8754466','江苏省');
insert into customers(cust_name,cust_tel,cust_address) values('书店d','0553-4563466','江苏省');
insert into customers(cust_name,cust_tel,cust_address) values('书店e','0553-2145212','江苏省');

insert into books(book_name,price) values('php',78);
insert into books(book_name,price) values('java',72);
insert into books(book_name,price) values('c++',72);
insert into books(book_name,price) values('C语言',56);
insert into books(book_name,price) values('sql',53);
insert into books(book_name,price) values('asp.net',69);
insert into books(book_name,price) values('asp',43);

insert into orders(isbn,cust_id,order_date) values(700001,1,getdate());
insert into orders(isbn,cust_id,order_date) values(700002,1,getdate());
insert into orders(isbn,cust_id,order_date) values(700003,2,getdate());
insert into orders(isbn,cust_id,order_date) values(700001,1,getdate());
insert into orders(isbn,cust_id,order_date) values(700003,1,getdate());
insert into orders(isbn,cust_id,order_date) values(700006,1,getdate());
insert into orders(isbn,cust_id,order_date) values(700001,2,getdate());
insert into orders(isbn,cust_id,order_date) values(700001,2,getdate());
insert into orders(isbn,cust_id,order_date) values(700002,2,getdate());
insert into orders(isbn,cust_id,order_date) values(700003,1,getdate());
insert into orders(isbn,cust_id,order_date) values(700003,9,getdate());--我下面是故意的,让大家看到效果
insert into orders(isbn,cust_id,order_date) values(7000025,9,getdate());

select a.order_id,a.isbn,b.book_name,b.price,c.cust_name,c.cust_tel,c.cust_address,a.order_date
from orders as a inner join books as b 
on
a.isbn=b.isbn inner join customers as c
on
a.cust_id=c.cust_id;


select a.order_id,a.isbn,b.book_name,b.price,c.cust_name,c.cust_tel,c.cust_address,a.order_date
from orders as a left join books as b 
on
a.isbn=b.isbn left join customers as c
on
a.cust_id=c.cust_id;


select a.order_id,a.isbn,b.book_name,b.price,c.cust_name,c.cust_tel,c.cust_address,a.order_date
from orders as a right join books as b 
on
a.isbn=b.isbn right join customers as c
on
a.cust_id=c.cust_id;

 

左表右表指哪些表?
select * from 
table_1 as a left join table_2 as c
on
a.id=b.id 
leftjoin table_3 as c
on
b.isbn=c.isbn


疑问中............
第一种认为:
认为最左边的表左表 table_1
右表就是除了左表的其它表,而不是最右边的表
所以这里的左表是 table_1,那右表是table_2,table_3

第二种认为:
table_1 是table_2的左表,table_2是table_3的左表

CSDN SQL专区提问,竟请您的加盟:http://community.csdn.net/Expert/topic/5696/5696608.xml?temp=.4501764

还请牛人指点!

最终答案:
table_1 左连接 table_2 以table_1为基础进行连接运算,得到新表即临时表(#table)。
#table     左连接 table_3 以table# 为基础进行连接运算,得到最终结果。
与我的总结一致!


以下是来自网上的关于关联说的比较好的文章(易懂)
来自:http://www.cnblogs.com/temptation/archive/2006/05/23/407328.aspx
表A记录如下:
aID                  aNum
1                     a20050111
2                     a20050112
3                     a20050113
4                     a20050114
5                     a20050115


表B记录如下:
bID                  bName
1                      2006032401
2                     2006032402
3                     2006032403
4                     2006032404
8                     2006032408

实验如下:
1.              left join
sql语句如下: 

SQL多表关联! inner join,left join ,right join - 十维空间 - SunriseSELECT * FROM A
SQL多表关联! inner join,left join ,right join - 十维空间 - Sunrise
LEFT JOIN B 
SQL多表关联! inner join,left join ,right join - 十维空间 - Sunrise
ON A.aID = B.bID

结果如下:
aID                  aNum                             bID                     bName
1                      a20050111                   1                         2006032401
2                      a20050112                   2                        2006032402
3                      a20050113                   3                        2006032403
4                      a20050114                   4                        2006032404
5                      a20050115                   NULL                 NULL

(所影响的行数为 5 行)

结果说明:
                  left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的.
换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.bID).
B表记录不足的地方均为NULL.


2.              right join
sql语句如下: 
SQL多表关联! inner join,left join ,right join - 十维空间 - SunriseSELECT * FROM A
SQL多表关联! inner join,left join ,right join - 十维空间 - Sunrise
RIGHT JOIN B 
SQL多表关联! inner join,left join ,right join - 十维空间 - Sunrise
ON A.aID = B.bID

结果如下:
aID                  aNum                             bID                     bName
1                      a20050111                   1                         2006032401
2                      a20050112                   2                        2006032402
3                      a20050113                   3                        2006032403
4                      a20050114                   4                        2006032404
NULL              NULL                          8                        2006032408

(所影响的行数为 5 行)

结果说明:
                  仔细观察一下,就会发现,和left join的结果刚好相反,这次是以右表(B)为基础的,A表不足的地方用NULL填充.


3.inner join
sql语句如下: 
SQL多表关联! inner join,left join ,right join - 十维空间 - SunriseSELECT * FROM A
SQL多表关联! inner join,left join ,right join - 十维空间 - SunriseINNERJOIN B 
SQL多表关联! inner join,left join ,right join - 十维空间 - Sunrise
ON A.aID = B.bID

结果如下:
aID                  aNum                             bID                     bName
1                      a20050111                   1                         2006032401
2                      a20050112                   2                        2006032402
3                      a20050113                   3                        2006032403
4                      a20050114                   4                        2006032404


结果说明:
                  很明显,这里只显示出了 A.aID = B.bID的记录.这说明inner join并不以谁为基础,它只显示符合条件的记录.


-----------------[以下为网上的一点资料]------------------ 
LEFT JOIN操作用于在任何的 FROM 子句中,组合来源表的记录。使用 LEFT JOIN 运算来创建一个左边外部联接。左边外部联接将包含了从第一个(左边)开始的两个表中的全部记录,即使在第二个(右边)表中并没有相符值的记录。 

语法:
FROM table1 LEFT JOIN table2 ON table1.field1 compopr table2.field2 

说明:
① table1, table2参数用于指定要将记录组合的表的名称。
② field1, field2参数指定被联接的字段的名称。且这些字段必须有相同的数据类型及包含相同类型的数据,但它们不需要有相同的名称。
③ compopr参数指定关系比较运算符:"=", "<", ">", "<=", ">=" 或 "<>"。
④ 如果在INNER JOIN操作中要联接包含Memo 数据类型或 OLE Object 数据类型数据的字段,将会发生错误。

分享到:
评论
1 楼 沧澜竹 2016-05-28  
受益1111111111111

相关推荐

Global site tag (gtag.js) - Google Analytics