2

我有多个网站在单个数据库上运行。

我的问题相关表结构如下:

USER
    - UserID
WEBSITE
    - WebsiteID
        - WebsiteName
LOGINRECORD
    - UserID
    - WebsiteID
    - LoginDate

我想计算通过网站登录的唯一用户数。

Website          NumberofUserLoggedinMorethanOnce 
abc.com                     15 
def.com                     25
4

3 回答 3

5

编辑:我误解了这个问题。应该是这样的:

SELECT WEBSITE.WebsiteName, count(*)
FROM WEBSITE 
INNER JOIN (
SELECT UserID, WebsiteID, count(*) FROM LOGINRECORD
GROUP BY UserID, WebsiteID
HAVING count(*) > 1) AS T1 ON t1.WebsiteID = WEBSITE.WebsiteID
GROUP BY WEBSITE.WebsiteName
于 2012-06-17T23:50:08.563 回答
3
SELECT WebsiteID,
       COUNT(*) AS cnt
  FROM (SELECT WebsiteID
          FROM LOGINRECORD
      GROUP BY WebsiteID,
               UserID
        HAVING count(*) > 1) x
GROUP BY WebsiteID
于 2012-06-17T23:55:06.647 回答
1

这是未经测试的,但试一试 -

select websitename, count(*)
from website
inner join
(
select userid, websiteid, count(*)
from loginrecord
inner join website on loginrecord.websiteid = website.websiteid
group by userid, websiteid
having count(*) > 1
) tbl
on tbl.websiteid = website.websiteid
group by websitename
于 2012-06-17T23:57:50.100 回答