Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I have following table
- | Id| source_url | target_url | event_type |
- |:---- |:------:| -----:| -----:|
- | 1| http://google.com/| http://mypage.com/example | 0 |
- | 1| http://google.com/| http://mypage.com/otherpage| 0 |
- | 2| http://google.com/| http://mypage.com/example | 0 |
- | 3| http://google.com/| http://mypage.com/example | 0 |
- | 4| http://mypage.com/example| http://otherdomain.com/example | 1 |
- | 5| http://mypage.com/example| http://otherdomain.com/example | 1 |
- | 5| http://mypage.com/otherpage| http://otherdomain.com/example | 1 |
- Here `event_type = 0` is Visit and `event_type = 1` is Click
- Using following query I can get **visits** on each url
- 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
- ;
- ### Visits
- | target_url | visits |
- |:---- |:------:|
- | http://mypage.com/example| 3|
- | http://mypage.com/otherpage | 1|
- And with following query i can get clicks
- 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
- ;
- ### Clicks
- | source_url | clicks |
- |:---- |:------:|
- | http://mypage.com/example| 2|
- | http://mypage.com/otherpage| 1|
- ### Desired Result
- I want my final result to be like below ***without using subquery*** (since original table contains more than 1 million rows and subquery takes too long)
- | url | clicks | visits |
- |:---- |:------:| -----:|
- | http://mypage.com/example| 2| 3|
- | http://mypage.com/otherpage| 1| 1|
- Right now, I feel like I have to create a temporary table and use `ON DUPLICATE KEY UPDATE` and use url as primary key, but I feel like there might be a better way and I couldn't figure out.
- ***Is there any way to achieve this result without using subquery and temporary table?***
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement