注册 登录
编程论坛 SQL Server论坛

请大家看看我的语句有什么问题[未解决]?

关小彤 发布于 2007-05-14 11:48, 836 次点击
select a.name,id_card,b.sname,sfzh,a.bknbcode_id,b.leibie into shuju from no07 a , zuihoujieguo b
where a.name=b.sname
and len(b.sfzh)=15 and len(a.id_card)=15 and a.id_card=b.sfzh and a.bknbcode_id=b.leibie
union
select a.name,id_card,b.sname,sfzh,a.bknbcode_id,b.leibie from forums_ksbm a ,zuihoujieguo b
where a.name=b.sname
and left(b.sfzh,6)=left(a.id_card,6)
and len(b.sfzh)=15 and len(a.id_card)=18
and substring(a.id_card,9,6)=substring(b.sfzh,7,6) and a.bknbcode_id=b.leibie
union
select a.name,id_card,b.sname,sfzh,a.bknbcode_id,b.leibie from forums_ksbm a , zuihoujieguo b
where a.name=b.sname
and len(b.sfzh)=18 and len(a.id_card)=18 and a.id_card=b.sfzh and a.bknbcode_id=b.leibie
union
select a.name,id_card,b.sname,sfzh,a.bknbcode_id,b.leibie from forums_ksbm a , zuihoujieguo b
where a.name=b.sname

and left(b.sfzh,6)=left(a.id_card,6)
and len(a.id_card)=15 and len(b.sfzh)=18
and substring(b.sfzh,9,6)=substring(a.id_card,7,6) and a.bknbcode_id=b.leibie


这个语句中查出shuju中有749条记录
然后
select * from shuju a, no07 b where a.name=no07.name and a.id_card =b.id_card
查出的纪录条数是401条


怎么会有这种情况呢?
为什么不是749条呢?

谢谢 :)

[此贴子已经被作者于2007-5-14 17:12:28编辑过]

10 回复
#2
cyyu_ryh2007-05-14 12:33
条件不一样当然查询出的就不一样了
#3
关小彤2007-05-14 13:21

不是,我想说的是第一个语句中选出的不是no07中的id_card吗?为什么会有不在no07表中的数据呢

#4
bygg2007-05-14 13:34
这么神奇???
#5
关小彤2007-05-14 13:40
请大家帮帮忙,我都晕了。而且我刚才
用select a.id_card,b.sfzh from no07 a,zuihoujieguo b where a.id_card=b.sfzh and a.bknbcode_id=b.leibie and a.name=b.sname
查出的记录条数竟然是1358
怎么会比上面查出的多呢
#6
关小彤2007-05-14 14:05
没人回答吗?谢谢
#7
bruce2007-05-14 14:40
以下是引用cyyu_ryh在2007-5-14 12:33:39的发言:
条件不一样当然查询出的就不一样了

就是这个问题,,
你想要查什么都还没弄明白呢

#8
关小彤2007-05-14 15:44
基本情况是这样的:两个表中都有身份证号,但是可能存在15位18位的情况,例如:一个人在no07中是15位的,但在另外一个表中是18位的
我想查出两个表中同一个人的信息
#9
Kendy1234562007-05-14 17:34

拿3个表把这些数据装下来 比一下不就知道为什么不同了

#10
关小彤2007-05-17 11:56
第一条语句的结果插入到一个表中时,有11条记录是重复的。奇怪
#11
bygg2007-05-17 14:11
你的语句可以改成
select a.name,id_card,b.sname,sfzh,a.bknbcode_id,b.leibie into shuju
from no07 a ,zuihoujieguo b
where a.name=b.sname
and len(b.sfzh)=15
and len(a.id_card)=15
and a.id_card=b.sfzh
and a.bknbcode_id=b.leibie
union
select a.name,id_card,b.sname,sfzh,a.bknbcode_id,b.leibie
from forums_ksbm a ,zuihoujieguo b
where a.name=b.sname
and
(
(
len(b.sfzh)=18
and len(a.id_card)=18
and a.id_card=b.sfzh
and a.bknbcode_id=b.leibie
)
or
(
(left(b.sfzh,6)=left(a.id_card,6)
and a.bknbcode_id=b.leibie)
and
(
(len(b.sfzh)=15 and len(a.id_card)=18 and substring(a.id_card,9,6)=substring(b.sfzh,7,6))
or
(len(a.id_card)=15 and len(b.sfzh)=18 and substring(b.sfzh,9,6)=substring(a.id_card,7,6))
)
)
)

不过为什么会出现相同的.你得看一下你的表中的数据是怎样的了.
1