这是查询
SELECT `mpv`.`member_id` as member_id, `screen_name`, `viewed_url`,
count(viewed_url) as viewed_url_cnt
FROM (`tracker` mpv)
JOIN `members` m ON `mpv`.`member_id`=`m`.`member_id`
GROUP BY `viewed_url`, `member_id`
它返回
+-----------+-------------+------------+----------------+
| member_id | screen_name | viewed_url | viewed_url_cnt |
+-----------+-------------+------------+----------------+
| 1 | admin | / | 1 |
| 1 | admin | /1 | 1 |
| 1 | admin | /2 | 1 |
| 1 | admin | 2 | 1 |
| 1 | admin | 3 | 2 |
| 2 | test | 4 | 1 |
+-----------+-------------+------------+----------------+
6 rows in set (0.12 sec)
我需要获取viewed_url_cnt
分组viewed_url
member_id
+-----------+-------------+------------+----------------+
| member_id | screen_name | viewed_url | viewed_url_cnt |
+-----------+-------------+------------+----------------+
| 1 | admin | 3 | 2 |
| 2 | test | 4 | 1 |
+-----------+-------------+------------+----------------+
6 rows in set (0.12 sec)
的所有记录。所以基于顶部的结果,我想得到
{{1}}
由于
答案 0 :(得分:1)
我可能会误解你想要的内容,但解决方案是使用内部查询为每个viewed_url_cnt
确定适当的member_id
,然后JOIN
到您的表:
SELECT *
FROM my_qry outer
JOIN
(SELECT member_id, MAX(viewed_url_cnt) AS max_cnt
FROM my_qry
GROUP BY member_id) inner
ON outer.member_id = inner.member_id AND outer.viewed_url_cnt = inner.max_cnt
(此处my_qry
是您用于生成原始表的查询。)
答案 1 :(得分:1)
我想以下是可能的:
SELECT * FROM (SELECT * FROM table GROUP BY name) as foo GROUP BY dept
用查询替换内部查询,然后您可以通过member_id获取max(seen_url_cnt)组。
如果失败,请告诉我..
答案 2 :(得分:1)
试试这个:
select s1.member_id, s1.screen_name, s1.viewed_url, s1.viewed_url_cnt from (
select t1.member_id, t1.screen_name, t1.viewed_url, count(*) viewed_url_cnt
from tracker t1
group by t1.member_id, t1.screen_name, t1.viewed_url
) as s1
join (
select s2.member_id, max(viewed_url_cnt) as viewed_url_max from (
select t1.member_id, count(*) viewed_url_cnt
from tracker t1
group by t1.member_id, t1.viewed_url
) as s2
group by s2.member_id
) as s3
on s1.member_id = s3.member_id and s1.viewed_url_cnt = s3.viewed_url_max