【发布时间】:2021-09-03 02:37:51
【问题描述】:
我有下表
| Id | source_url | target_url | event_type |
|---|---|---|---|
| 1 | googlecom | mypageurl/example | 0 |
| 2 | googlecom | mypageurl/otherpage | 0 |
| 3 | googlecom | mypageurl/example | 0 |
| 4 | googlecom | mypageurl/example | 0 |
| 5 | mypageurl/example | otherdomain/example | 1 |
| 6 | mypageurl/example | otherdomain/example | 1 |
| 7 | mypageurl/otherpage | otherdomain/example | 1 |
这里event_type = 0 是访问,event_type = 1 是点击
使用以下查询,我可以在每个网址上获得 visits
SELECT
target_url,
COUNT(target_url) AS visits
FROM
tbl_events
WHERE
event_type = 0 AND target_url <> ''
GROUP BY
target_url
ORDER BY
visits
DESC
;
访问次数
| target_url | visits |
|---|---|
| mypageurl/example | 3 |
| mypageurl/otherpage | 1 |
通过以下查询,我可以获得点击次数
SELECT
source_url,
COUNT(source_url) AS clicks
FROM
event_tracking
WHERE
event_type = 1 AND source_url <> ''
GROUP BY
source_url
ORDER BY
clicks
DESC
;
点击次数
| source_url | clicks |
|---|---|
| mypageurl/example | 2 |
| mypageurl/otherpage | 1 |
想要的结果
我希望我的最终结果如下所示不使用子查询(因为原始表包含超过 100 万行且子查询耗时过长)
| url | clicks | visits |
|---|---|---|
| mypageurl/example | 2 | 3 |
| mypageurl/otherpage | 1 | 1 |
现在,我觉得我必须创建一个临时表并使用 ON DUPLICATE KEY UPDATE 并使用 url 作为主键,但我觉得可能有更好的方法,我想不通。
有什么方法可以在不使用子查询和临时表的情况下达到这个结果?
【问题讨论】:
-
这是支点。使用条件聚合。
标签: mysql large-data