选择每个关系的最高记录

时间:2015-06-02 08:46:41

标签: mysql database relationship

我需要获得所有用户访问量最大的网站

我有以下表格:

页:

id link_page
1  link 1
2  link 2
3  Link 3

用户:

id name
1 Joseph
2 Cris
3 Byram

pages_user:

  user_id  page_id
     1         1
     1         1
     1         1
     2         1
     2         1
     2         2

应获得以下内容:

user_id | most visited page id
 1              1
 2              1

到目前为止,我做了类似的事情:

SELECT count(pu.page_id) as counter, pu.user_id 
FROM page_user pu
INNER JOIN page p on p.id = pu.page_id
INNER JOIN users u on u.id = pu.user_id
GROUP BY pu.user_id, pu.page_id 
ORDER BY counter DESC

但我会将所有记录归还。

因为我可以获得所有用户访问量最大的网站吗?

来自智利的问候。

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

SELECT
  t.user_id,
  t.page_id AS most_visited_page_id
FROM (
      SELECT 
        pu.user_id,
        pu.page_id,
        COUNT(*) AS visitCount
      FROM page_user pu
        INNER JOIN page p ON p.id = pu.page_id
        INNER JOIN user u ON u.id = pu.user_id
      GROUP BY pu.user_id, pu.page_id
      ORDER BY pu.user_id, visitCount DESC, rand() <-- In case of multiple
                          page with same count it will give random page or you can check 
                          it with most recent visit with date if any -->

    ) AS t
GROUP BY t.user_id

这是sqlfiddle

答案 1 :(得分:0)

SELECT id, (SELECT page_id FROM page_user 
WHERE user_id = id GROUP BY page_id 
ORDER BY COUNT(page_id) DESC  LIMIT 1) FROM USER

尝试此查询。您可以修改以显示所有必填字段

编辑:要查看匹配数,请使用以下查询。

SELECT id, (SELECT page_id FROM page_user 
WHERE user_id = id GROUP BY page_id 
ORDER BY COUNT(page_id) DESC  LIMIT 1) page_id, (SELECT COUNT(page_id) cnt FROM page_user 
WHERE user_id = id GROUP BY page_id 
ORDER BY cnt DESC  LIMIT 1) cnt FROM USER

答案 2 :(得分:0)

以下是一种方法,请考虑以下内容

mysql> select * from user;
+------+--------+
| id   | name   |
+------+--------+
|    1 | Joseph |
|    2 | Cris   |
|    3 | Byam   |
+------+--------+
3 rows in set (0.00 sec)

mysql> select * from page;
+------+-----------+
| id   | link_page |
+------+-----------+
|    1 | link 1    |
|    2 | link 2    |
|    3 | link 3    |
+------+-----------+
3 rows in set (0.00 sec)

+---------+---------+
| user_id | page_id |
+---------+---------+
|       1 |       1 |
|       1 |       1 |
|       1 |       1 |
|       2 |       1 |
|       2 |       1 |
|       2 |       2 |
|       3 |       2 |
|       3 |       2 |
|       3 |       2 |
|       3 |       1 |
|       3 |       3 |
|       3 |       3 |
+---------+---------+
12 rows in set (0.00 sec)


select 
u.id as user_id,
p.id as `most visited page id`
from user u join ( 
  select count(*) as cnt,user_id,page_id from pages_user group by user_id,page_id
)x on x.user_id = u.id 
left join(
 select count(*) as cnt,user_id,page_id from pages_user group by user_id,page_id
)y on x.user_id = y.user_id and y.cnt > x.cnt
join page p on p.id = x.page_id 
where y.user_id is null ;

+---------+----------------------+
| user_id | most visited page id |
+---------+----------------------+
|       1 |                    1 |
|       2 |                    1 |
|       3 |                    2 |
+---------+----------------------+