日期:2014-05-18  浏览次数:20445 次

???简单表查询问题 ???
表 T1

name danwei
感康 盒
维C 盒

表 liushui
id name danwei riqi
1 感康 盒 2010-01-01
2 感康 盒 2012-02-15
3 维C 盒 2012-02-18

如何查询出liushui表 2010-01-01 到 2012-02-15 期间段内未包含T1表的其它产品数据,结果为

name danwei
维C 盒


------解决方案--------------------
SQL code

select a.*
from t1 a left join liushui b 
    on a.name = b.name and a.danwei = b.danwei
    and b.riqi between '2010-01-01' and '2012-02-15'
where b.id is null

------解决方案--------------------
SQL code
declare @t table(name varchar(10),danwei varchar(20))

insert into @t

select '感康', '盒' union all
select '维C', '盒' 


declare @t2 table(id int , name varchar(10), danwei varchar(20), riqi datetime)

insert into @t2

select 1, '感康','盒','2010-01-01' union all
select 2,'感康', '盒', '2012-02-15' union all
select 3, '维C','盒','2012-02-18'



select  distinct b.* from @t2 a,@t b 
where a.riqi between '2010-01-01' and '2012-02-15' 
and a.name != b.name