表-1IP
202.84.52.77
205.14.52.77.32
118.121.149.128
表-2开始IP 结束IP 区域
001.000.000.000 001.255.255.255 美国
002.006.190.056 002.006.190.063 英国
003.000.000.000 003.255.255.255 美国
005.163.066.080 005.163.066.095 瑞典想得到如下表,就是表1根据ip字段判断在表2哪个ip段范围内,然后得出所在区域IP 区域
202.84.52.77 广东
205.14.52.77.32 山西
118.121.149.128 .......
202.84.52.77
205.14.52.77.32
118.121.149.128
表-2开始IP 结束IP 区域
001.000.000.000 001.255.255.255 美国
002.006.190.056 002.006.190.063 英国
003.000.000.000 003.255.255.255 美国
005.163.066.080 005.163.066.095 瑞典想得到如下表,就是表1根据ip字段判断在表2哪个ip段范围内,然后得出所在区域IP 区域
202.84.52.77 广东
205.14.52.77.32 山西
118.121.149.128 .......
解决方案 »
- 如何使用T-SQL对EXCEL报表进行操作(drop table)
- 关于insert命令的问题
- 求助:关于如何在sqlserver2005中使用的纯sql语句进行数据库的操作的问题?
- 为什么我老是连接不上数据库啊
- 救命呀!ms-sql Server为什么装不起呀!
- ■■■求SQL语句对一个表的统计■■■
- 一条我看不明的sql语句,请各位指点一二,谢谢!
- 如果主键是标识(自动增加列),在有并发操作情况怎么读出刚insert的行?
- SQL-Sever 2000的数据库能否做成独立的文件(不安装SQL也能被程序调用?)
- 有关数据排列的棘手问题
- SQL同一张单据里按里面的款号进行筛选
- 初学sql server存储过程。请赐教。
http://topic.csdn.net/u/20080414/16/48e0e77b-5a6c-49e8-a423-372cf42b77ff.html
根据各进制向十进制转换的规则:
N=an*256^(n-1)+...a1*256^(1-1),
可以知道,202.84.52.77的十进制数为
202*256^3+84*256^2+52*256^1+77*256^0=3397910605
然后再与表2开始结束IP作比较
比如表2第一列
开始IP:1*256^3+0*256^2+0*256^1+0*256^0= 16777216
结束IP:1*256^3+255*256^2+255*256^1+255*256^0=33554431declare @ip as varchar(50)
set @ip = '220.113.49.43'
select 区域 from 表2
where
cast(PARSENAME(@ip , 4) as bigint) * 256 * 256 * 256 +
cast(PARSENAME(@ip , 3) as bigint) * 256 * 256 +
cast(PARSENAME(@ip , 2) as bigint) * 256 +
cast(PARSENAME(@ip , 1) as bigint)
>=
cast(PARSENAME(startip , 4) as bigint) * 256 * 256 * 256 +
cast(PARSENAME(startip , 3) as bigint) * 256 * 256 +
cast(PARSENAME(startip , 2) as bigint) * 256 +
cast(PARSENAME(startip , 1) as bigint) and
cast(PARSENAME(@ip , 4) as bigint) * 256 * 256 * 256 +
cast(PARSENAME(@ip , 3) as bigint) * 256 * 256 +
cast(PARSENAME(@ip , 2) as bigint) * 256 +
cast(PARSENAME(@ip , 1) as bigint)
<=
cast(PARSENAME(endip , 4) as bigint) * 256 * 256 * 256 +
cast(PARSENAME(endip , 3) as bigint) * 256 * 256 +
cast(PARSENAME(endip , 2) as bigint) * 256 +
cast(PARSENAME(endip , 1) as bigint)