易截截图软件、单文件、免安装、纯绿色、仅160KB

外连接sql的一个问题

当在内连接查询中加入条件时,无论是将它加入到join子句,还是加入到where子句,其效果是完全一样的,但对于外连接情况就不同了。当把条件加入到 join子句时,会返回外连接表的全部行,然后使用指定的条件返回第二个表的行。如果将条件放到where子句中,将会首先进行连接操作,然后使用where子句对连接后的行进行筛选。例如:
(1)
select u.id, u.p_plan_id, u.unit_name, p.id, p.plan_name
  from t_baidu_p_units u
  left outer join t_baidu_p_plans p on u.p_plan_id = p.id
                                   and p.id = 3176
 where 1 = 1
 order by u.p_plan_id desc
它会返回t_baidu_p_units表中所有行,t_baidu_p_plans表中符合join条件的字段不为null
(2)
select u.id, u.p_plan_id, u.unit_name, p.id, p.plan_name
  from t_baidu_p_units u
  left outer join t_baidu_p_plans p on u.p_plan_id = p.id
                                   and p.id = 3176
 where p.id is null
 order by u.p_plan_id desc
查询的结果集中没有符合join条件的数据
(3)
select u.id, u.p_plan_id, u.unit_name, p.id, p.plan_name
  from t_baidu_p_units u
  left outer join t_baidu_p_plans p on u.p_plan_id = p.id
                                   and p.id = 3176
 where p.id is not null
 order by u.p_plan_id desc
结果集中只包含join条件的数据
分析一下:
  (1)where1=1对其进行过滤时,由于都符合此条件,因此没有改变结果集
  (2)根据where p.id is null对于join后的结果集进行筛选,凡是p.id不为null的都要过滤掉,因此自然没有p.id=3176的结果
  (


相关文档:

sql 2005 存储过程分页 java 代码

 create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',         
@pagesize int output,--每页显示记录条数
@currentpage int output,--第几页
@orderid nvarchar(50),--主键排序
@sort int,--排序方式,1表示升序,0表示降序排列 ......

航空公司管理系统(VC++ 与SQL 2005)

系统环境:Windows 7
软件环境:Visual C++ 2008 SP1 +SQL Server 2005
本次目的:编写一个航空管理系统
      这是数据库课程设计的成果,虽然成绩不佳,但是作为我用VC++ 以来编写的最大程序还是传到网上,以供参考。用VC++ 做数据库设计并不容易,但也不是不可能。以下是我的程序界面,后面 ......

SQL中EXISTS的用法


比如在Northwind数据库中
有一个查询为
SELECT c.CustomerId, CompanyName
from Customers c
WHERE EXISTS(
SELECT OrderID from Orders o
WHERE o.CustomerID = cu.CustomerID)
这里面的EXISTS是如何运作呢?子查询返回的是OrderId字段,可是外面的查询要找的是CustomerID和CompanyName字段,这两个字段肯 ......

240个国外SQL Server高手的博客集萃

Aaron Bertrand
Adam Machanic
All Things SQL Server
Allen Kinsel - SQL DBA
Allen White
Amit Bansal writes...
Andrew Fryer's Blog
Andrew Kelly
Andy Leonard
Anything and Everything IT
Arcane Code
Arnie Rowland: Ramblings of a Harried Technogeek
B.I. for the SQL Guy
Bart Duncan's SQL Weblog ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号