select (n个字段) 
from b 
where id in (select id from a where date between @StardDate and @EndDate and houseid=@houseid and void='0')
上面是我写的正确的sql语句;下面的sql语句加了两个表c,d;到c,d里面根据id把name查询出来,查询结果就跟前面的不一样了
理论上是一样的,根据id求name四个表:a,b,c,d
关键字:a.id=b.id;
       a.houseid=c.houseid; 
       b.itemid=d.itemid;select (n个字段)
from a,b,c,d
where a.date between @StardDate and @EndDate and a.houseid=@houseid and a.void='0' and b.id=a.id and  b.itemid=c.itemid and a.divid=d.divid
各位看看,我的sql语句改什么写啊?

解决方案 »

  1.   

    四个表:a,b,c,d 
    关键字:a.id=b.id; 
          a.divid=d.divid;
          b.itemid=c.itemid;
      

  2.   

    估计是重复了吧?select distinct (n个字段)
    from a,b,c,d
    where a.date between @StardDate and @EndDate and a.houseid=@houseid and a.void='0' and b.id=a.id and  b.itemid=c.itemid and a.divid=d.divid
      

  3.   

    select (n个字段)
    from b
    left join a on b.id=a.id
    left join c on b.itemid=c.itemid 
    left join d on a.divid=d.divid
    where a.date between @StardDate and @EndDate and a.houseid=@houseid and a.void='0'
      

  4.   

    谢谢上面两位的回复2楼查询后生成的结果集是有重复的,不能直接去掉重复行的,里面是有重复3楼查询跟我上面的结果是一样的,都比我第一次查询的结果多了几条记录多表查询就是根据id到c,d表把name带出来
      

  5.   

    难道是我写的上面和下面的两条sql语句不能达到一样的效果但是调节都是一样的
    where a.date between @StardDate and @EndDate and a.houseid=@houseid and a.void='0' and b.id=a.id --and  b.itemid=c.itemid and a.divid=d.divid
      

  6.   

    注释掉以后,两句就是一样的为什么加上去就不行呢?select (n个字段)
    from a,b--,c,d
    where a.date between @StardDate and @EndDate and a.houseid=@houseid and a.void='0' and b.id=a.id --and  b.itemid=c.itemid and a.divid=d.divid
    select (n个字段) 
    from b 
    where id in (select id from a where date between @StardDate and @EndDate and houseid=@houseid and void='0')