当前位置: 代码迷 >> Sql Server >> sql语句 如何实现类似 在like语句里嵌套查询
  详细解决方案

sql语句 如何实现类似 在like语句里嵌套查询

热度:7   发布时间:2016-04-27 11:15:57.0
sql语句 怎么实现类似 在like语句里嵌套查询
表A 有三个字段 no , id ,p-id 
现要查询 以p-id为a 的no 开头的 id ,只用一条sql查询 ,怎么实现?
eg :p-id为3 的行 no 字段值为 0f,现要查以0f开头的 (like '0f%')的行的 id。
其中 p-id 是由参数传人的,不是给定的。
select categories_id from mc_categories where categories_status = 1 and categories_no like '(select categories_no where parent_id={$parent_id})%' 错在哪?
like 语句里是不是不可以嵌套其他查询,如果不行,那要实现类似功能该怎么做

------解决方案--------------------
SQL code
--你的表是什么,换了就可以了select categories_id from mc_categories     where categories_status = 1        and exists (select categories_no from 你的表 where parent_id='{$parent_id}'        and mc_categories.categories_no like 你的表.categories_no+'%')
------解决方案--------------------
这个我没试过这样
但我可以给你个建议
用一个文本框 或是什么控件
parent_id = parent.text 
然后再 select categories_id from mc_categories where categories_status = 1 and categories_no like 'parent_id%'
------解决方案--------------------
SQL code
select categories_id from mc_categories where categories_status = 1 and categories_no like (select categories_no where [email protected]_id)+'%'
------解决方案--------------------
SQL code
--mssql写法declare @parent_id varchar(12)set @parent_id='000123456789'select categories_id from mc_categories     where categories_status = 1        and exists (select categories_no from tb where [email protected]_id        and mc_categories.categories_no like tb.categories_no+'%')
  相关解决方案