有where和没有where的区别
下面两个SQL语句的区别在哪里?
select B.FItemNum,A.Famount from B left join A on A. FitemNum=B. FitemNum And A.FdataType =3 And A.Ftype = 1 And A.FdataClass = 0;
select B.FItemNum,A.Famount from B left join A on A. FitemNum=B. FitemNum where A.FdataType =3 And A.Ftype = 1 And A.FdataClass = 0;

解决方案 »

  1.   

    第一个是符合条件的联查 如果不符合条件对应的值是Null
    第二个是过滤掉不符合条件的记录
      

  2.   

    有where的过滤掉不符合条件的记录,可能得不到记录,这个很容易看出来。没有where的是条件联查,这句话是什么意思呢?
      

  3.   

    这道题目是我面试金蝶遇见的。左连接后面还跟AND,我在实际项目中还真没遇见过。一般都是Where进行条件过滤
      

  4.   

    1、 on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义
    (必须返回左边表的记录)了,条件不为真的就全部过滤掉。
      

  5.   

    2楼的意思是on后面的所有的AND作为条件来进行左连接?
      

  6.   

    --> 测试数据:@ta
    declare @ta table([id] int,[size] int)
    insert @ta
    select 1,10 union all
    select 2,20 union all
    select 3,30--> 测试数据:@tb
    declare @tb table([size] int,[name] varchar(3))
    insert @tb
    select 10,'AAA' union all
    select 20,'BBB' union all
    select 20,'CCC'select * from @ta a left join @tb b on a.[size]=b.[size] and a.[id]=1
    -------------------结果---------
    /*
    id          size        size        name
    ----------- ----------- ----------- ----
    1           10          10          AAA
    2           20          NULL        NULL
    3           30          NULL        NULL
    */select * from @ta a left join @tb b on a.[size]=b.[size] where a.[id]=1
    ------------------------结果------------
    /*
    id          size        size        name
    ----------- ----------- ----------- ----
    1           10          10          AAA
    */
    ----------------------------------------
      

  7.   

    第一个里的 on 后面的条件意义不大 因为它使用了Left join 返回的是B表的相关记录
    第二个经过左连接后 再用where中A表限定的条件 过滤 
    就是说 A表的条件起了过滤的作用~
      

  8.   

    还有一个SQL题目,不另外开贴了,等下整理后发上来再问大家,向大家学习了
      

  9.   

    打开T_Product表,请写一个sql语句。 
    输入某一年,即查询出该年内1-12个月中的商品数目,没有商品的月显示为0,显示效果如下所示(可建立临时表进行左外连接)。
    T_Product表内有三个字段,其中时间显示年月日
    产品ID ,产品名称 ,添加时间输入2007年,则显示如下
    月份 商品个数 
    1    ### 
    2    ### 
    3    ### 
    4    ### 
    5    ### 
    6    ### 
    7    ### 
    8    ### 
    9    ### 
    10   ### 
    11   ### 
    12   ###
      

  10.   

    我已经找到正确答案了,---测试数据---
    if object_id('[T_Product]') is not null drop table [T_Product]
    go
    create table [T_Product]([产品ID] int,[产品名称] varchar(3),[添加时间] datetime)
    insert [T_Product]
    select 1,'abc','2009-1-2' union all
    select 13,'abc','2008-12-2' union all
    select 14,'abc','2009-12-2'
      
    ---查询---
    declare @year int;
    set @year=2009
    select 月份=1,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=1),0)
    union all
    select 月份=2,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=2),0)
    union all
    select 月份=3,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=3),0)
    union all
    select 月份=4,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=4),0)
    union all
    select 月份=5,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=5),0)
    union all
    select 月份=6,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=6),0)
    union all
    select 月份=7,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=7),0)
    union all
    select 月份=8,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=8),0)
    union all
    select 月份=9,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=9),0)
    union all
    select 月份=10,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=10),0)
    union all
    select 月份=11,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=11),0)
    union all
    select 月份=12,商品个数=isnull((select count(1) from T_Product where year(添加时间)=@year and month(添加时间)=12),0)---结果---
    月份          商品个数        
    ----------- ----------- 
    1           1
    2           0
    3           0
    4           0
    5           0
    6           0
    7           0
    8           0
    9           0
    10          0
    11          0
    12          1(所影响的行数为 12 行)
      

  11.   

    是啊,我也觉得这样写不好,Beirut,你把你的方法分享一下啊
      

  12.   

    --> 测试数据:@ta
    if object_id('[T_Product]') is not null drop table [T_Product] 
    go 
    create table [T_Product]([产品ID] int,[产品名称] varchar(3),[添加时间] datetime) 
    insert [T_Product] 
    select 1,'abc','2009-1-2' union all 
    select 13,'abc','2008-12-2' union all 
    select 14,'abc','2009-12-2' --假如输入 2009
    select id,[产品名称],[添加时间] from 
    (select (number+1) as id  from master.dbo.spt_values where type = 'p' AND number < 12) a
    left join 
    (select * from [T_Product] where year([添加时间])='2009')  b on a.id=datepart(mm,[添加时间])
    /*
    id          产品名称 添加时间
    ----------- ---- -----------------------
    1           abc  2009-01-02 00:00:00.000
    2           NULL NULL
    3           NULL NULL
    4           NULL NULL
    5           NULL NULL
    6           NULL NULL
    7           NULL NULL
    8           NULL NULL
    9           NULL NULL
    10          NULL NULL
    11          NULL NULL
    12          abc  2009-12-02 00:00:00.000(12 行受影响)
    */
      

  13.   

    题目只要求统计月份和当月产品总量,当月没有产品的,产品总量记为0
    我当时写的是这样,这样的话如果当月没有产品,就没有统计了
    SELECT DATEPART(MONTH,createdate),COUNT(DATEPART(MONTH,createdate))
    FROM dbo.t_product
    WHERE DATEPART(yyyy,createdate) = '2007'
    GROUP BY DATEPART(MONTH,createdate)
      

  14.   

    --> 测试数据:@ta
    if object_id('[T_Product]') is not null drop table [T_Product] 
    go 
    create table [T_Product]([产品ID] int,[产品名称] varchar(3),[添加时间] datetime) 
    insert [T_Product] 
    select 1,'abc','2009-1-2' union all 
    select 13,'abc','2008-12-2' union all 
    select 14,'abc','2009-12-2' 
    ------------------查询开始---------------------
    --假如输入 2009
    select a.[月份],isnull(b.[商品个数],0) as[商品个数] from 
    (select (number+1) as [月份]  from master.dbo.spt_values where type = 'p' AND number < 12) a
    left join 
    (select month([添加时间]) as [月份],count(1) as [商品个数] from [T_Product] where year([添加时间])='2009' group by month([添加时间])) b 
    on a.[月份]=b.[月份]
    ----------------------------结果--------------------------------
    /*
    月份          商品个数
    ----------- -----------
    1           1
    2           0
    3           0
    4           0
    5           0
    6           0
    7           0
    8           0
    9           0
    10          0
    11          0
    12          1(12 行受影响)*/
      

  15.   

    Beirut,你太厉害了,结贴了。
      

  16.   

    select B.FItemNum,A.Famount from B left join A on A. FitemNum=B. FitemNum And A.FdataType =3 And A.Ftype = 1 And A.FdataClass = 0; 
    select B.FItemNum,A.Famount from B left join A on A. FitemNum=B. FitemNum where A.FdataType =3 And A.Ftype = 1 And A.FdataClass = 0; 第二个语句比第一个区别是查询出的记录必须存在B表中
      

  17.   

    master.dbo.spt_values,这个表还能用,多谢