日期:2014-05-17  浏览次数:20637 次

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 parent_id=@parent_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 parent_id=@parent_id
        and mc_categories.categories_no like tb.categories_no+'%')