日期:2014-05-18  浏览次数:20496 次

找出含有某个字符的最大id
数据库中有字段
id pathcode
1 null
2 /1
3 /1/2
4 /1/2
5 /1/2
6 /1/2/4
7 /1/2/4
8 /1/2/4/5

找出数据库中含有2个 "/" 最大id,此数据中最大id=5 ,请问Sql语句怎么写?

------解决方案--------------------
SQL code
create table tb
(id int,
 pathcode varchar(50)
)
insert into tb values(1,'')
insert into tb values(2,'/1')
insert into tb values(3,'/1/2')
insert into tb values(4,'/1/2')
insert into tb values(5,'/1/2')
insert into tb values(6,'/1/2/4')
insert into tb values(7,'7 /1/2/4')
insert into tb values(8,'/1/2/4/5')

 

select max(id)    from tb

 where   len(pathcode) - len(replace(pathcode,'/',''))  =2
 
-----------
5

(1 行受影响)