嵌套查询sql语句中能用like吗? 我在做查询。语句如下。 select count(*) as count ,cm_member.mem_hotel from cm_member where cm_member.mem_hotel like(select htl_sid from cm_hotel where cm_hotel.htl_country='20090312708827321319' AND cm_hotel.htl_city='20090312339003708918' AND cm_hotel.sys_deleted='N' AND htl_status='Published' AND htl_draft='') group by cm_member.mem_hotel order by cm_member.mem_hotel ASC
其中 (select htl_sid from cm_hotel where cm_hotel.htl_country='20090312708827321319' AND cm_hotel.htl_city='20090312339003708918' AND cm_hotel.sys_deleted='N' AND htl_status='Published' AND htl_draft='') 语句查出来的是酒店的ID号,而在cm_member这张表中mem_hotel字段,存的就是酒店ID,不过,这个mem_hotel可以存好几个酒店ID,例如,酒店ID1,酒店ID2 他们中间用逗号分隔, 所以,我做查询,就想第一步得到酒店ID,然后在cm_member表中mem_hotel字段用like来比较酒店ID。
但是我用LIKE报的错误是: Subquery returns more than 1 row 翻译过来是:子查询返回多个行1。
select count(*) as count ,cm_member.mem_hotel from cm_member
where charindex(',' + (select top 1 htl_sid from cm_hotel where cm_hotel.htl_country='20090312708827321319' AND cm_hotel.htl_city='20090312339003708918' AND cm_hotel.sys_deleted='N' AND
htl_status='Published' AND htl_draft='')+ ',' , ',' + cm_member.mem_hotel + ',') > 0 group by cm_member.mem_hotel order by cm_member.mem_hotel ASC
------解决方案-------------------- 这样写肯定不行的啊,你参照楼上的charindex的写法来做
------解决方案-------------------- like后的select相当于一个list啊 如果含有多个记录like铁定不行啊
like改成in看看呢
------解决方案--------------------
------解决方案-------------------- 好像不行吧..
------解决方案-------------------- 当子查询 后面的语句 返回多条记录是用 IN 不能用 = 号
------解决方案-------------------- 我写的是给 sql server 用的,你用的该不是 mysql 吧,如果是自己转换一下
------解决方案-------------------- 杯具...
select a.mem_hotel from cm_member a inner join cm_hotel b on charindex(b.htl_sid,a.mem_hotel)>0 group by a.mem_hotel order by a.mem_hotel ASC