繁体   English   中英

如何获取SQL中的行数?

[英]How to get the number of rows in SQL?

我有一个用户数据库,需要帮助以形成查询。

Table = users
fields = sign_in_count , last_sign_in_at

这是两个查询,可为我提供以下结果:-

select *
from users
where last_sign_in_at >= '2015-08-01' and sign_in_count != ''

给了我131000行

select *
from users
where last_sign_in_at between '2015-05-01' and '2015-09-11' and
      sign_in_count <> ''

给了我203000行

因此在05/15到08/15之间至少登录过一次的72,000个用户中,用户数之间的差异。

我需要知道这72k用户中多次登录的用户数量。

假设users表具有user_id列,下面的查询将起作用。

    select count(user_id) from
     (
    select user_id --replace it with appropriate user identifier column
    from (select * from users
    where last_sign_in_at between '2015-05-01' and '2015-09-11' 
    and sign_in_count != ''
    and user_id not in (select user_id from users 
    where last_sign_in_at >= '2015-08-01' and sign_in_count!='')
     ) t
    group by user_id 
    having count(*) > 1
    ) t1

出什么问题了

Select count(*) from users
where last_sign_in_at  between '2015-05-01' and '2015-08-01'
and sign_in_count > 1

还是我错过了什么?

您要查询所需时间范围内已登录一次以上的用户总数。

但是您只是在查询以查找'2015-05-01'和'2015-08-01'之间的用户总数以及sign_in_count <>''。

考虑采取两个的其余部分where在派生表的条件为count()

select count(*) 
from
   (select * from users 
    where last_sign_in_at > '2015-05-01' 
    and last_sign_in_at < '215-08-01'
    and sign_in_count != '')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM