博主资料

留言 加为好友 收藏

用户名:  yanchengyang

公告

本博客原创文章,欢迎转载,转载请注明出处

日 历

2008 7.6 Sun
  12345
6789101112
13141516171819
20212223242526
2728293031  
«» 2008 - 7 «»

个人统计

用户名: yanchengyang
等级: 初来乍到
威望: 495
积分: 1180
在线时间: 50 小时
日志总数: 84
评论数量: 82
访问次数: 363457
建立时间: 2006-05-16
RSS订阅       手机访问

最新评论

文章搜索

友情链接

最近访问的人:

海阔天空
2008-07-05 09:34:55
杨喜的学习心得
2008-07-04 22:16:47
pyq1985
2008-07-04 09:59:20
2008-07-03 16:46:15
中国风景园林网
2008-07-03 14:22:43
菜鸟上路
2008-07-01 10:26:23
yujing
2008-06-30 14:50:29
hfhongxia
2008-06-24 12:57:24
程序人家
2008-06-24 11:28:03
caroline
2008-06-22 17:16:06

日志文章

2007年04月14日 16:47:10

SQL查询中的连接

以下是SQL中的常用连接查询的简单写法,采用SQL-Server示例数据库pubs和Northwind

以下是详细代码:

use pubs
--内连接

select titleauthor.au_id,au_lname,title_id from authors inner join
titleauthor on titleauthor.au_id=authors.au_id
--内连接 (另一种写法,和上例结果一样)
select titleauthor.au_id,au_lname,title_id from authors ,
titleauthor where titleauthor.au_id=authors.au_id

--左外连接,将取出左表中的所有记录,右表没有对应将以Null进行添充

select titleauthor.au_id,au_lname,title_id from authors left join
titleauthor on titleauthor.au_id=authors.au_id


--右外连接,将取出右表中的所有记录,左表没有对应将以Null进行添充

select titleauthor.au_id,au_lname,title_id from authors right join
titleauthor on titleauthor.au_id=authors.au_id


--自连接,就是自己连接自己,
--如下题:请选择员工编号,员工姓名,及员工直接上级的编号,姓名
--解决方法:因为员工上级也是公司的员工,也在本表出现,这就要采用连接,而数据出自同一张表
--故采用自连接,自连接主要是给一张表起两个别名,假想成两张表来做,一切OK,
--代码如下:
use northwind


select a.employeeid,a.lastname,a.reportsto,b.lastname from employees a
 left join employees b on a.reportsto=b.employeeid

select employeeid,lastname,reportsto from employees

Tags: SQL   连接查询  

类别: 数据库 |  评论(0) |  浏览(2433) |  收藏
-20楼 [楼主][匿名] Says:
-21楼 [楼主][匿名] Says:
-22楼 [楼主][匿名] Says:
-23楼 [楼主][匿名] Says:
-24楼 [楼主][匿名] Says:
-25楼 [楼主][匿名] Says:
-26楼 [楼主][匿名] Says:
-27楼 [楼主][匿名] Says:
-28楼 [楼主][匿名] Says:
-29楼 [楼主][匿名] Says:
发表评论