当前位置: 代码迷 >> Sql Server >> 容易的SQL,不会
  详细解决方案

容易的SQL,不会

热度:35   发布时间:2016-04-24 08:50:01.0
简单的SQL,不会

select name,(select count(*) from table1 where id in (table2.idSum)) from table2

当table2的idSum =2 是对的
但 当idSum =2,3,4,5 的时候就没数据了。。真是搞晕了。
------解决思路----------------------
idSum =2时,语句相当于:select name,(select count(*) from table1 where id in ('2')) from table2,会有记录。
idSum =2,3,4,5时,语句相当于:select name,(select count(*) from table1 where id in ('2,3,4,5')) from table2,不会有记录。
------解决思路----------------------
select name,(select count(*) from table1 where charindex(','+rtrim(id)+',',','+table2.idSum+',')>0) from table2
  相关解决方案