当前位置: 代码迷 >> Sql Server >> ?简单表查询有关问题 ?
  详细解决方案

?简单表查询有关问题 ?

热度:103   发布时间:2016-04-27 14:03:46.0
???简单表查询问题 ???
表 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 @tselect '感康', '盒' union allselect '维C', '盒' declare @t2 table(id int , name varchar(10), danwei varchar(20), riqi datetime)insert into @t2select 1, '感康','盒','2010-01-01' union allselect 2,'感康', '盒', '2012-02-15' union allselect 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
  相关解决方案