0

假设我有这个 sql 查询

SELECT max(times_viewed) as m, member_id, (select count(viewed_url) from exp_member_page_tracker  where member_id=mpv.member_id)
FROM `exp_member_page_tracker` mpv
group by member_id

我需要计算所有结果。我这样做

SELECT count(member_id) from ( SELECT max(times_viewed) as m, member_id, (select count(viewed_url) from exp_member_page_tracker where member_id=mpv.member_id)
FROM `exp_member_page_tracker` mpv
group by member_id) as m2

但我不确定这如何有效。您能否建议我如何以最佳方式计算结果?

谢谢

4

1 回答 1

1

对于您的子查询,我是否遗漏了什么,这不是更高效的结果吗?在 sqlfiddle 试过:

http://sqlfiddle.com/#!2/a03cf/9

你的:

SELECT member_id, max(times_viewed) as max_times_viewed 
, (select count(viewed_url) from exp_member_page_tracker  where member_id=mpv.member_id) as countOfViewedUrl
FROM `exp_member_page_tracker` mpv
group by member_id;

选择:

SELECT member_id, max(times_viewed) as max_times_viewed
, count(viewed_url) as countOfViewedUrl
FROM `exp_member_page_tracker` mpv
group by member_id;
于 2012-04-14T20:44:27.767 回答