易截截图软件、单文件、免安装、纯绿色、仅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的结果
  (


相关文档:

PL/SQL初学者必读:几十个实用的PL/SQL

第一阶段
Q.编写一个PL/SQL程序块以显示所给出雇员编号的雇员的详细信息。
A.
 DECLARE
 erec emp%ROWTYPE;
BEGIN
 SELECT * INTO erec from emp  WHERE empno=&雇员编号;
 DBMS_OUTPUT.PUT_LINE('EmpNo' || ' ' || 'Ename' || ' '|| 'Job' || ' ' || 'Manager' || ' ' || 'HireDate' ......

EXCEL转数据到SQL(已有表结构)

insert into Country123 ([Country_Id], [Region_ID], [Country_EN_Name], [Country], [Country_ALL_ID], [Country_Order_Id]) select [Country_Id], [Region_ID], [Country_EN_Name], [Country], [Country_ALL_ID], [Country_Order_Id] from openrowset( 'Microsoft.Jet.OLEDB.4.0', 'EXCEL 5.0;HDR=YES;IMEX=1; DATABASE= ......

SQL Server中临时表与表变量的区别

我们在数据库中使用表的时候,经常会遇到两种使用表的方法,分别就是使用临时表及表变量。在实际使用的时候,我们如何灵活的在存储过程中运用它们,虽然它们实现的功能基本上是一样的,可如何在一个存储过程中有时候去使用临时表而不使用表变量,有时候去使用表变量而不使用临时表呢?
  临时表
  临时表与永久表相似, ......

SQL参数化

SqlCommand com = new SqlCommand("select * from myuser where username=@UserName and password=@Pwd", con);
           
            com.Parameters.Add(new SqlParameter("@UserN ......

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号