i have weblog captures visits website:
table weblog id logdate browser ipaddr inet_aton_ip 1 2013-05-01 chrome 42.61.66.124 708657788 2 2013-05-01 chrome 217.9.192.99 3641294848 3 2013-05-02 firefox 77.79.58.77 1297037901 ...etc...
i downloaded ip2nation database lets me country based on inet_aton_ip value:
mysql> select * ip2nation ip<708657788 order ip desc limit 1; +-----------+---------+ | ip | country | +-----------+---------+ | 708575232 | sg | +-----------+---------+ mysql> select * ip2nation ip<3641294848 order ip desc limit 1; +------------+---------+ | ip | country | +------------+---------+ | 3641286656 | uk | +------------+---------+ ... , on ...
the ip column in ip2nation marks ip boundary, search not precise, i.e. equal comparison not work. structure makes sense, subnet 42.61.66.x, don't have list 255 similar entries.
using c# routine like
datatable dt = [a sql function select * weblog] foreach (datarow row in dt.rows) { long ipnum = int.parse(row["inet_aton_ip"].tostring()); string cty = [a sql function select country using ipnum] }
i can following table:
id logdate browser ipaddr inet_aton_ip country 1 2013-05-01 chrome 42.61.66.124 708657788 sg 2 2013-05-01 chrome 217.9.192.99 3641294848 uk 3 2013-05-02 firefox 77.79.58.77 1297037901 lt
i wonder if can generate above table using 1 sql instead of 2 sqls, in c# routine? tried playing subselects , joins each time thrown off "ip less x order ip desc limit 1" portion. appreciate pointers sql experts here.
if have ip2nation
table can join both tables this:
select w.*, ip.country weblog w join ip2nation ip on w.inet_aton_ip = ip.ipp
output:
╔════╦════════════════════════════╦═════════╦══════════════╦══════════════╦═════════╗ ║ id ║ logdate ║ browser ║ ipaddr ║ inet_aton_ip ║ country ║ ╠════╬════════════════════════════╬═════════╬══════════════╬══════════════╬═════════╣ ║ 1 ║ may, 01 2013 00:00:00+0000 ║ chrome ║ 42.61.66.124 ║ 708657788 ║ sg ║ ║ 2 ║ may, 01 2013 00:00:00+0000 ║ chrome ║ 217.9.192.99 ║ 3641294848 ║ uk ║ ║ 3 ║ may, 02 2013 00:00:00+0000 ║ firefox ║ 77.79.58.77 ║ 1297037901 ║ lt ║ ╚════╩════════════════════════════╩═════════╩══════════════╩══════════════╩═════════╝
Comments
Post a Comment