1.这种检索方式正常
select ID, NAME from yjmc where ID IN ('8126130308','8126130208')
--------------------------
查询结果:
ID NAME
8126130308 采气管理三区
8126130208 采气管理二区
2. 查询 为什么返回是空?
select ID, NAME from yjmc where ID IN
(select '('''+ REPLACE(KHDW,',',''',''')+''')' as khdw from SYS_KHZB where KMDM='1003')
---------------------------
查询结果:
ID NAME
3.分解第二步
(select '('''+ REPLACE(KHDW,',',''',''')+''')' as khdw from SYS_KHZB where KMDM='1003')
---------------------------
查询结果:
khdw
('8126130308','8126130208')
谁给讲讲2里面的SQL语句错在哪里,出不来结果?
分享到:更多
------解决方案-------------------- 因为,第2种方法,
select ID, NAME from yjmc where ID IN
(select '('''+ REPLACE(KHDW,',',''',''')+''')' as khdw from SYS_KHZB where KMDM='1003')
select ID, NAME from yjmc
where exists(select 1
from SYS_KHZB
where KMDM='1003'
and charindex(','+yjmc.id+',',','+KHDW+',')>0 )
------解决方案-------------------- 其实关键是你的khdw列是用逗号分割的字符串,所以用这种办法来处理,虽然也有其他的办法,但其他的写法跟复杂 ------解决方案-------------------- select ''''+ REPLACE(KHDW,',',''',''')+'''' as khdw from SYS_KHZB where KMDM='1003'
这个结果是什么?你的ID是int类型吗? ------解决方案--------------------