免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1066 | 回复: 1
打印 上一主题 下一主题

问一个关于数据库查询的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-21 20:33 |只看该作者 |倒序浏览
本帖最后由 bjtulq 于 2012-03-21 20:33 编辑
  1. 我有四个字段:date(日期),ip,port,flow(流量数据)--一天之内有多次插入
  2. 怎样根据date和ip统计流量呢?
  3. 比如有如下:
  4. 2012-03-20 192.168.0.1 20 1000
  5. 2012-03-20 192.168.0.1 21 2000
  6. 2012-03-20 192.168.0.1 22 3000
  7. .....
  8. 如何得到:
  9. --date--    ---ip---    -flow-
  10. 2012-03-20 192.168.0.1   6000

  11. 非常感谢大家的帮助了
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-21 22:35 |只看该作者
1,创建表:
mysql> show create table accessLog\G
*************************** 1. row ***************************
       Table: accessLog
Create Table: CREATE TABLE `accessLog` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `time` date NOT NULL,
  `ip` int(10) unsigned NOT NULL,
  `port` int(10) unsigned NOT NULL,
  `flow` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=gbk
1 row in set (0.00 sec)

2,插入测试数据:

mysql> insert into accessLog values(null,current_date(), INET_ATON('192.168.0.1'),20,1000);
mysql> insert into accessLog values(null,current_date(), INET_ATON('192.168.0.1'),21,2000);
mysql> insert into accessLog values(null,current_date(), INET_ATON('192.168.0.1'),22,3000);
mysql> insert into accessLog values(null,current_date(), INET_ATON('192.168.0.2'),23,3000);
mysql> insert into accessLog values(null,current_date(), INET_ATON('192.168.0.2'),24,3000);
mysql> insert into accessLog values(null,current_date(), INET_ATON('192.168.0.2'),25,3000);

3,你所想要的sql和测试结果。 可以根据需要,再加一个索引。
mysql> select time as date,inet_ntoa(ip) as ip, sum(flow) as flow  from accessLog group by date,ip;
+------------+-------------+------+
| date       | ip          | flow |
+------------+-------------+------+
| 2012-03-21 | 192.168.0.1 | 6000 |
| 2012-03-21 | 192.168.0.2 | 9000 |
+------------+-------------+------+
2 rows in set, 1 warning (0.00 sec)



评分

参与人数 1信誉积分 +1 收起 理由
ruochen + 1 很给力!

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP