两个表查询,急~~~

simmi 2006-11-28 05:20:51
a表
sf bs
admin 1
北京市 0
天津市 0
河北省 0
山西省 0
内蒙古自治区 0
辽宁省 0
吉林省 0
黑龙江省 0
上海市 0
江苏省 0
安徽省 0
浙江省 0
.....

b表
name sheng sbsj
name1 河北省 2006-11-21 03:00:00
name2 河北省 2006-11-22 03:00:00
name3 北京市 2006-11-23 03:00:00
name4 北京市 2006-11-24 03:00:00
name5 北京市 2006-11-25 03:00:00
name6 河北省 2006-11-26 03:00:00
name7 河北省 2006-11-26 17:00:00
name8 河北省 2006-11-27 03:00:00
name9 河北省 2006-11-27 17:00:00


结果表:
a.sf name sbsj
北京市 name5 2006-11-25 03:00:00
天津市 null null
河北省 name9 006-11-27 17:00:00
山西省 null null
内蒙古自治区 null null
辽宁省 null null
吉林省 null null
黑龙江省 null null
上海市 null null
江苏省 null null
安徽省 null null
浙江省 null null
.....


条件:a表=0,b表中最新的数据,a.sf=b.sheng,a表sf在b表中没有结果的情况下,读出null值

...全文
159 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2006-11-28
  • 打赏
  • 举报
回复
if object_id('pubs..A') is not null
drop table a
go
create table A(sf nvarchar(10), bs bit)
insert A select 'admin', 1
union all select '北京市', 0
union all select '天津市', 0
union all select '河北省', 0
union all select '山西省', 0
union all select '内蒙古自治区', 0
union all select '辽宁省', 0
union all select '吉林省', 0
union all select '黑龙江省', 0
union all select '上海市', 0
union all select '江苏省', 0
union all select '安徽省', 0
union all select '浙江省', 0

if object_id('pubs..b') is not null
drop table b
go
create table B(name nvarchar(10), sheng nvarchar(10), sbsj datetime)
insert B select 'name1', '河北省', '2006-11-21 03:00:00'
union all select 'name2', '河北省', '2006-11-22 03:00:00'
union all select 'name3', '北京市', '2006-11-23 03:00:00'
union all select 'name4', '北京市', '2006-11-24 03:00:00'
union all select 'name5', '北京市', '2006-11-25 03:00:00'
union all select 'name6', '河北省', '2006-11-26 03:00:00'
union all select 'name7', '河北省', '2006-11-26 17:00:00'
union all select 'name8', '河北省', '2006-11-27 03:00:00'
union all select 'name9', '河北省', '2006-11-27 17:00:00'

select a.sf , isnull(m.name,null) as name , isnull(m.sbsj,null) as sbsj from a
left join
(select b.* from b ,(select sheng , max(sbsj) as sbsj from b group by sheng) t where b.sheng = t.sheng and b.sbsj = t.sbsj) m
on a.sf = m.sheng
where a.bs = 0

drop table a
drop table b


sf name sbsj
---------- ---------- ------------------------------------------------------
北京市 name5 2006-11-25 03:00:00.000
天津市 NULL NULL
河北省 name9 2006-11-27 17:00:00.000
山西省 NULL NULL
内蒙古自治区 NULL NULL
辽宁省 NULL NULL
吉林省 NULL NULL
黑龙江省 NULL NULL
上海市 NULL NULL
江苏省 NULL NULL
安徽省 NULL NULL
浙江省 NULL NULL

(所影响的行数为 12 行)

marco08 2006-11-28
  • 打赏
  • 举报
回复
create table A(sf nvarchar(10), bs bit)
insert A select 'admin', 1
union all select '北京市', 0
union all select '天津市', 0
union all select '河北省', 0
union all select '山西省', 0
union all select '内蒙古自治区', 0
union all select '辽宁省', 0
union all select '吉林省', 0
union all select '黑龙江省', 0
union all select '上海市', 0
union all select '江苏省', 0
union all select '安徽省', 0
union all select '浙江省', 0


create table B(name nvarchar(10), sheng nvarchar(10), sbsj datetime)
insert B select 'name1', '河北省', '2006-11-21 03:00:00'
union all select 'name2', '河北省', '2006-11-22 03:00:00'
union all select 'name3', '北京市', '2006-11-23 03:00:00'
union all select 'name4', '北京市', '2006-11-24 03:00:00'
union all select 'name5', '北京市', '2006-11-25 03:00:00'
union all select 'name6', '河北省', '2006-11-26 03:00:00'
union all select 'name7', '河北省', '2006-11-26 17:00:00'
union all select 'name8', '河北省', '2006-11-27 03:00:00'
union all select 'name9', '河北省', '2006-11-27 17:00:00'

select A.sf, tmp.name, tmp.sbsj from A
left join(
select sheng, max(name) as name, max(sbsj) as sbsj from B group by sheng
)tmp on A.sf=tmp.sheng
where bs=0


--result
sf name sbsj
---------- ---------- ------------------------------------------------------
北京市 name5 2006-11-25 03:00:00.000
天津市 NULL NULL
河北省 name9 2006-11-27 17:00:00.000
山西省 NULL NULL
内蒙古自治区 NULL NULL
辽宁省 NULL NULL
吉林省 NULL NULL
黑龙江省 NULL NULL
上海市 NULL NULL
江苏省 NULL NULL
安徽省 NULL NULL
浙江省 NULL NULL

(12 row(s) affected)
marco08 2006-11-28
  • 打赏
  • 举报
回复
select A.sf, tmp.name, tmp.sbsj from A
left join(
select sheng, max(name) as name, max(sbsj) as sbsj from B group by sheng
)tmp on A.sf=tmp.sheng
where bs=0
pengda1i 2006-11-28
  • 打赏
  • 举报
回复
楼主测试下,1楼的不对,只有两条记录

pengda1i 2006-11-28
  • 打赏
  • 举报
回复
select a.sf,b1.name,b1.sbsj
from a left join b b1
on a.sf=b1.sheng and b1.sbsj =(select max(sbsj) from b where sheng=b1.sheng)
where a.bs=0
dulei115 2006-11-28
  • 打赏
  • 举报
回复
select a.sf, b.name, b.sbsj
from a表 a left join b表 b on a.bs = b.sheng
where a.bs = 0
and b.sbsj = (select max(sbsj) from b表 where sheng = b.sheng)

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧