首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

如何做这种多表查询带条件判断的

2012-09-28 
怎么做这种多表查询带条件判断的SQL code我要查询小于或等于5月份的数据,对两个表里面的数据进行查询,条件

怎么做这种多表查询带条件判断的

SQL code
我要查询小于或等于5月份的数据,对两个表里面的数据进行查询,条件先查询表2里在面的数据,如果表2里面没有了再查表1里面的。判断小于或等于5月份,条件可以这样写convert(varchar(7),a.MontrDate,120)>=convert(varchar(7),‘2012-05-01’,120)/*表2Numbers  post  MontrDate001       12   2012-05-01003       12   2012-04-03表1Numbers  post  001       23   003       24004       36   这样的表结果要查询出来的结果Numbers  post 001       12  003       12  004       36  */


[解决办法]
SQL code
;with tt2 as(select Numbers,  post from 表2 where convert(varchar(7),MontrDate,120)>='2012-05')select * from tt2union allselect * from 表1 tt1 where not exists(select 1 from tt2 where Numbers=tt1.Numbers) 

热点排行