[为了增加解答的机率,我不得不重复发一条] 在两个表间进行 SELECT 操作的 问题,我想了一天都没有结果,很急阿,恳请请各位高手帮忙解决

LEO0071983 2006-09-04 03:32:02
下面是我的两个表

表一:
Chapter node isopen
1 NULL 1
1 1 1
1 2 0
2 1 0
3 NULL 1
3 1 0
....

表二
id Chapter node
1001 1 Null
1002 1 Null
1008 1 1
1009 1 1
1010 2 1
1011 2 1
1012 2 1
1013 2 1
1014 2 1
....
我想最终获得这样一个表
id Chapter node
1001 1 Null
1002 1 Null
1008 1 1
1009 1 1

即:根据 表二中每一条记录的chapter 和 node ,找到表一中的相应的行,如果 isopen = 1,
则 将表二中该行保留
============================
恳请各位高手帮帮忙啊!
小弟 万分感谢!
...全文
278 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
insnowind 2006-09-04
  • 打赏
  • 举报
回复
解决了就散点分,嘿嘿!!!
LEO0071983 2006-09-04
  • 打赏
  • 举报
回复
谢谢 rea1gz !我很欣赏您写的sql结构,很整洁优美
我原来的代码如下
select a.* from 表二 a,表一 b
where
(a.Chapter=b.Chapter and a.node=b.node)
or
(a.Chapter=b.Chapter and a.node is null and b.node is null)
and
b.isopen=1

但是总是得不到想要的结果!

谢谢 wangtiecheng 在你那我学到了 isnull地用法,我在实际应用中很需要这个函数
=========
一开机就看到问题被解决了,真是高兴啊!
谢谢每一个人
bugchen888 2006-09-04
  • 打赏
  • 举报
回复
SELECT b.*
FROM [表二] b, [表一] a
WHERE b.Chapter=a.Chapter
AND b.node=a.node
AND a.isopen=1
fxf66 2006-09-04
  • 打赏
  • 举报
回复
create table t1(Chapter int,node int,isopen int)
create table t2(id int,Chapter int,node int)

insert into t1
select 1,null,1 union all
select 1,1,1 union all
select 1,2,0 union all
select 2,1,0 union all
select 3,null,1 union all
select 3,1,0

insert into t2
select 1001,1,null union all
select 1002,1,null union all
select 1008,1,1 union all
select 1009,1,1 union all
select 1010,2,1

select * from t1
select * from t2

select b.*
from t1 a,t2 b
where a.chapter=b.chapter
and a.isopen=1
and (a.node =b.node or (a.node is null and b.node is null))

drop table t1,t2
rea1gz 2006-09-04
  • 打赏
  • 举报
回复
同理
isnull(A.node,0)=isnull(B.node,0)
rea1gz 2006-09-04
  • 打赏
  • 举报
回复
isnull(a.node,0)=isnull(b.node,0)
是不对的
因为a.node=null b.node=''是不应该认为相等的

冒牌有时候就是这么讨厌的吹毛求疵,呵呵
xyxfly 2006-09-04
  • 打赏
  • 举报
回复
select B.* from 表一 A inner join 表二 B on A.chapter=B.chapter and isnull(A.node,0)=isnull(B.node,0) where A.isopen=1

楼主没有试试上面各位给的答案呀? 怎么开两个贴...
chyliu 2006-09-04
  • 打赏
  • 举报
回复
select B.* from
表一 as A,表二 as B
Where A.Chapter = B.Chapter and
Isnull(A.node,'') = Isnull(B,node,'') and
A.isopen = 1
vovo2000 2006-09-04
  • 打赏
  • 举报
回复
select a.*
from 表二 a join 表一 b
on
a.Chapter=b.Chapter
and IsNull(a.node,0)=IsNull(b.node,0)
where
b.isopen=1
OracleRoob 2006-09-04
  • 打赏
  • 举报
回复
--改

select b.*
from 表一 a inner join 表二 b on a.chapter=b.chapter and isnull(a.node,0)=isnull(b.node,0)
where A.isopen=1
OracleRoob 2006-09-04
  • 打赏
  • 举报
回复
select b.*
from 表一 a inner join 表二 b on a.chapter=b.chapter and isnull(a.node,0)=isnull(b.node,0)
where b.isopen=1
OracleRoob 2006-09-04
  • 打赏
  • 举报
回复
select a.*
from 表一 a inner join 表二 b on a.chapter=b.chapter and isnull(a.node,'')=isnull(b.node,'')
where b.isopen=1
rea1gz 2006-09-04
  • 打赏
  • 举报
回复
select a.* from 表二 a,表一 b
where a.Chapter=b.Chapter
and (a.node=b.node or
(a.node is null and b.node is null)
)
and b.isopen=1

27,580

社区成员

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

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