让我们假设我有这个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
但我不确定这是多么有效。能否请您建议我如何以最佳方式计算结果?
由于
答案 0 :(得分: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;