mysql> Select a.start_ip,a.end_ip,a.country,a.province,a.city ,
-> @id as sFlag ,
-> (case when @c<> mname then @id:=@id+1 end) as none,
-> @c:=mname
-> from (select start_ip,end_ip,country,province,city,concat(country,province,city) as mname From t_wsd_ip order by start_ip asc ) a,(select @c:='',@id:=1 ) b
-> ;
原始数据:
+----------+----------+--------------+----------+---------+-------+------+----------------------------+
| start_id | end_id | country | province | city | sFlag | none | @c:=mname |
+----------+----------+--------------+----------+---------+-------+------+----------------------------+
| 16777216 | 16777217 | 大利亚 | unknown | unknown | 1 | 2 | 澳大利亚unknownunknown |
| 16777217 | 16777218 | 澳大利亚 | unknown | unknown | 2 | NULL | 澳大利亚unknownunknown |
| 16777218 | 16777471 | 澳大利亚 | unknown | unknown | 2 | NULL | 澳大利亚unknownunknown |
| 1677472 | 16778239 | 中国 | 福建 | unknown | 2 | 3 | 中国福建unknown |
| 16778240 | 16779263 | 澳大利亚 | unknown | unknown | 3 | 4 | 澳大利亚unknownunknown |
| 16779264 | 16781311 | 中国 | 广东 | unknown | 4 | 5 | 中国广东unknown |
| 16781312 | 16781823 | 日本 | unknown | unknown | 5 | 6 | 日本unknownunknown |
| 16781824 | 16782335 | 日本 | unknown | unknown | 6 | NULL | 日本unknownunknown |
| 16782336 | 16782847 | 日本 | unknown | unknown | 6 | NULL | 日本unknownunknown |
| 16782848 | 16783359 | 日本 | unknown | unknown | 6 | NULL | 日本unknownunknown |
| 16783360 | 16783871 | 日本 | unknown | unknown | 6 | NULL | 日本unknownunknown |
| 16783872 | 16784383 | 日本 | unknown | unknown | 6 | NULL | 日本unknownunknown |
+----------+----------+--------------+----------+---------+-------+------+----------------------------+
12 rows in set (0.00 sec)
要求得到 按照 相邻的start_id排序的相同地区、省份 的最大最小值,
注意是相邻的 分组 ,
需求是:
如果相邻的几条 country province city 相等的话,我取这几条的start_ip end_ip 的最大值和最小值。
比如说:刚才文件的,1 2 3 是一组,4 是一组,5 是一组,6 是一组,7 8 9 10 是一组。
Select A.country,A.province,A.city, max(start_ip),min(start_ip),max(end_ip),min(end_ip) From
( Select (case when @c<>mname then @id:=@id+1 end) as none,
@id as sFlag,@c:=mname, a.start_ip,a.end_ip,a.country,a.province,a.city
from (select start_ip,end_ip,country,province,city,concat(country,province,city) as mname From t_wsd_ip order by start_ip asc ) a,(select @c:='',@id:=1 ) b ) A
Group by A.country,A.province,A.city,A.sFlag
+--------------+----------+---------+---------------+---------------+-------------+-------------+
| country | province | city | max(start_ip) | min(start_ip) | max(end_ip) | min(end_ip) |
+--------------+----------+---------+---------------+---------------+-------------+-------------+
| 中国 | 广东 | unknown | 16779264 | 16779264 | 16781311 | 16781311 |
| 中国 | 福建 | unknown | 16777472 | 16777472 | 16778239 | 16778239 |
| 澳大利亚 | unknown | unknown | 16777218 | 16777216 | 16777471 | 16777217 |
| 澳大利亚 | unknown | unknown | 16778240 | 16778240 | 16779263 | 16779263 |
| 日本 | unknown | unknown | 16783872 | 16781312 | 16784383 | 16781823 |
+--------------+----------+---------+---------------+---------------+-------------+-------------+
可以看到 澳大利亚的条数 是2条,2个分组 。。。
+--------------+----------+---------+---------------+---------------+-------------+-------------+
| country | province | city | max(start_ip) | min(start_ip) | max(end_ip) | min(end_ip) |
+--------------+----------+---------+---------------+---------------+-------------+-------------+
| 中国 | 广东 | unknown | 16779264 | 16779264 | 16781311 | 16781311 |
| 中国 | 福建 | unknown | 16777472 | 16777472 | 16778239 | 16778239 |
| 澳大利亚 | unknown | unknown | 16778240 | 16777216 | 16779263 | 16777217 |
| 日本 | unknown | unknown | 16783872 | 16781312 | 16784383 | 16781823 |
+--------------+----------+---------+---------------+---------------+-------------+-------------+
4 rows in set (0.00 sec)+--------------+----------+---------+---------------+---------------+-------------+-------------+
| country | province | city | max(start_ip) | min(start_ip) | max(end_ip) | min(end_ip) |
+--------------+----------+---------+---------------+---------------+-------------+-------------+
| 中国 | 广东 | unknown | 16779264 | 16779264 | 16781311 | 16781311 |
| 中国 | 福建 | unknown | 16777472 | 16777472 | 16778239 | 16778239 |
| 澳大利亚 | unknown | unknown | 16778240 | 16777216 | 16779263 | 16777217 |
| 日本 | unknown | unknown | 16783872 | 16781312 | 16784383 | 16781823 |
+--------------+----------+---------+---------------+---------------+-------------+-------------+
4 rows in set (0.00 sec)
阅读(2081) | 评论(0) | 转发(0) |