免费注册 查看新帖 |

Chinaunix

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

问分组统计的SQL语句! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-03 16:47 |只看该作者 |倒序浏览
表:
mysql> desc friend;
+----------+------------------+------+-----+---------+-------+
| Field      | Type                         | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| id            | int(10) unsigned    | NO   | PRI    |         |       |
| friend      | int(10) unsigned    | NO   | PRI    |         |       |
+----------+------------------+------+-----+---------+-------+

mysql> desc pt_loginfo;
+--------------+----------------------+------+-----+---------+-------+
| Field           | Type                    | Null | Key | Default | Extra |
+--------------+----------------------+------+-----+---------+-------+
| userid         | int(10) unsigned  | NO   | PRI |            |       |
| signup_time| int(10) unsigned | NO   | MUL | 0        |       |
+--------------+----------------------+------+-----+---------+-------+

其中  friend. id =  pt_loginfo.userid

我的SQL:
mysql> select count(friend.friend) AS cnt from passport.pt_loginfo,friend.friend where pt_loginfo.userid = friend.id AND pt_loginfo.signup_time >
UNIX_TIMESTAMP('2008-10-01 00:00:00') group by friend.friend having count(cnt) > 20;
. . . . . .
|    28 |
|    21 |
|    41 |
|    25 |
|    21 |
|    38 |
|    23 |
+-------+
53406 rows in set (1 hour 5 min 45.63 sec)
我只想要 53406 这个总数;
就是 pt_loginfo.userid = friend.id 的时候, friend.friend 列 分组统计后值大于 20 的总数。

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
2 [报告]
发表于 2009-03-03 16:50 |只看该作者
select count(*) from
(
select count(friend.friend) AS cnt from passport.pt_loginfo,friend.friend where pt_loginfo.userid = friend.id AND pt_loginfo.signup_time >
UNIX_TIMESTAMP('2008-10-01 00:00:00') group by friend.friend having count(cnt) > 20

)
as a;

论坛徽章:
0
3 [报告]
发表于 2009-03-03 16:57 |只看该作者
select count(*) from
(
select count(friend.friend) AS cnt from passport.pt_loginfo,friend.friend where pt_loginfo.userid = friend.id AND pt_loginfo.signup_time >
UNIX_TIMESTAMP('2008-10-01 00:00:00') group by friend.friend having count(cnt) > 20

)
as a;


多谢!!!

多问一句
就是 pt_loginfo.userid = friend.id 的时候, friend.friend 列 分组统计后值大于 20 的总数。

我这个想要的结果在那条SQL逻辑上没有错误吧 ?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP