I dette eksempel vil jeg vise dig nogle nyttige MySql-forespørgsler til at vise enhver bruger- eller produktanalyserapport om dine produkters ydeevne.
Antag, at du har et produktbasewebsted, og du skal beregne antallet af klik og unikke klik på produktet, eller du skal vise en grafvisning af månedlige eller årlige klik eller unikke klik. Til det formål er disse forespørgsler meget nyttige.
Her har jeg en produktanalysetabel med nogle analytiske data.
produktanalytisk
Product_Id | Klik | IP | Oprettet | Opdateret |
---|---|---|---|---|
1 | 1 | 192.168.1.1 | 2016-01-01 00:00:00 | 2016-01-01 00:00:00 |
1 | 1 | 192.168.1.1 | 2016-01-01 00:00:00 | 2016-01-01 00:00:00 |
2 | 1 | 192.168.2.1 | 2016-01-02 00:00:00 | 2016-01-02 00:00:00 |
.. | .. | 1.. | ... | ... |
.. | .. | 1.. | ... | ... |
.. | .. | 1.. | ... | ... |
Fra denne tabel skal jeg hente klik og unikke klik på produktdag, uge, måned, år.
Henter data dagligt
SELECT DATE(created) AS date, COUNT(click) AS click, COUNT(DISTINCT(ip)) AS unique_click FROM product_analytic WHERE created BETWEEN '2016-01-01 00:00:00' AND '2016-01-31 23:59:59' GROUP BY date ORDER BY date |
OUTPUT:
Henter data ugentligt
SELECT DATE_FORMAT(created, '%X-%V') AS date, COUNT(click) AS click, COUNT(DISTINCT(ip)) AS unique_click FROM product_analytic WHERE created BETWEEN '2016-01-01 00:00:00' AND '2016-01-31 23:59:59' GROUP BY date ORDER BY date |
Henter data efter månedlig
SELECT DATE_FORMAT(created, '%Y-%m') AS date, COUNT(click) AS click, COUNT(DISTINCT(ip)) AS unique_click FROM product_analytic WHERE created BETWEEN '2016-01-01 00:00:00' AND '2016-01-31 23:59:59' GROUP BY date ORDER BY date |
Henter data efter årligt
SELECT DATE_FORMAT(created, '%Y') AS date, COUNT(click) AS click, COUNT(DISTINCT(ip)) AS unique_click FROM product_analytic WHERE created BETWEEN '2016-01-01 00:00:00' AND '2016-01-31 23:59:59' GROUP BY date ORDER BY date |