读书人

找出含有某个字符的最大id,该怎么解决

发布时间: 2012-02-06 15:52:45 作者: rapoo

找出含有某个字符的最大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 行受影响) 

读书人网 >SQL Server

热点推荐