表的结构和内容如下:
SchoolId,int (学校的ID)
SchoolIP,nvarchar (学校的IP地址段,用|分割,允许多个IP地址段)内容:
SchoolID,SchoolIP
------- ---------
1,172.18.18|172.18.19|172.18.20
2,172.18.1|172.18.2
3,172.18.3|172.18.4
4,172.18.181|172.18.182|172.18.21
…………
…………
50,172.18.9~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
现在想求个语句,给一个IP地址,该IP地址形式是172.X.X。如172.18.1。根据该IP地址查出该IP对应的SchoolID。(发现用link会出问题,比如172.18.1,返回的是SchoolID=1,2,4。正确的话,应该是返回SchoolID=2)

解决方案 »

  1.   

    create table a
    (SchoolID int,
    SchoolIP varchar(50))
    insert a select 1,'172.18.18|172.18.19|172.18.20'
    union all select 2,'172.18.1|172.18.2'
    union all select 3,'172.18.3|172.18.4'
    union all select 4,'172.18.181|172.18.182|172.18.21'
    select * from a where charindex('|172.18.1|','|'+schoolip+'|')>0
      

  2.   


    create table #T(SchoolID int,SchoolIP varchar(100))insert #T (SchoolID,SchoolIP)
    select 1,'172.18.18|172.18.19|172.18.20' union all 
    select 2,'172.18.1|172.18.2' union all 
    select 3,'172.18.3|172.18.4' union all 
    select 4,'172.18.181|172.18.182|172.18.21'declare @IP varchar(200)set @IP='172.18.1'select * from #T where charindex('|' + @IP + '|','|' + SchoolIP + '|')>0drop table #T