简体   繁体   English

Mysql PHP - 如何计算每天记录的个人加权分数

[英]Mysql PHP - How to calculate individual weighted score for each day record

I have table in mysql database with 4 columns as hashtag_tid,user_id,activity_date and daily_score.我在mysql数据库中有4列作为hashtag_tid、user_id、activity_date和daily_score的表。

| hashtag_tid | user_id | activity_date | daily_score |
|        1260 |    2488 |    1441823400 |           1 |
|        1280 |    2488 |    1441823400 |           1 |
|        1229 |    5042 |    1441823400 |          23 |
|        1237 |    5042 |    1441823400 |          23 |
|        1260 |    5042 |    1441823400 |          42 |
|        1280 |    5042 |    1441823400 |          67 |
|        1802 |    5042 |    1441823400 |          23 |
|        1852 |    5042 |    1441823400 |          23 |
|        1260 |    2488 |    1441477800 |           1 |
|        1280 |    2488 |    1441477800 |           1 |
|        1229 |    5042 |    1441477800 |          23 |
|        1237 |    5042 |    1441477800 |          23 |
|        1260 |    5042 |    1441477800 |          42 |
|        1280 |    5042 |    1441477800 |          67 |
|        1802 |    5042 |    1441477800 |          23 |
|        1852 |    5042 |    1441477800 |          23 |

In the above table, for the combination of hashtag_tid and user_id I need to apply some weightage to daily_score for each day.在上表中,对于 hashtag_tid 和 user_id 的组合,我需要对每天的 daily_score 应用一些权重。

Weightage will be vary for each day like, for today's date daily_score should be daily_score*1, for yesterday daily_score*0.8, It will be different for each day.每天的权重会有所不同,例如今天的daily_score 应该是daily_score*1,昨天的daily_score*0.8,每天都会有所不同。

Please help me to solve this using MYSQL and PHP.请帮助我使用 MYSQL 和 PHP 解决这个问题。

Thank you谢谢

Following can be a possible solution if you will have a constant number of past days to calculate the weighted score.如果您有固定的过去天数来计算加权分数,则以下可能是一个可能的解决方案。

SELECT `hashtag_tid`, `user_id`, 
  CASE 
    WHEN DATE_FORMAT(FROM_UNIXTIME(`activity_date`), '%Y-%m-%d') = CURDATE() THEN sum(`daily_score`)*1 
    WHEN DATE_FORMAT(FROM_UNIXTIME(`activity_date`), '%Y-%m-%d') = subdate(CURDATE(), 1) THEN sum(`daily_score`)*3 
    ELSE 0 END AS weighted_score 
FROM `tabel` 
Group By `hashtag_tid`, `user_id`, `activity_date`

This is only calculated for last 2 days.这仅计算最近 2​​ 天。

Possible solution if you want to keep the weightings in a table so they can be easily changed in future:-如果您想将权重保留在表格中以便将来可以轻松更改,则可能的解决方案:-

SELECT hashtag_tid, SUM(h.daily_score + w.weighting)
FROM hashtags h
INNER JOIN weighting w
ON DATEDIFF(CURDATE(), DATE(FROM_UNIXTIME(h.activity_date))) = w.days_diff
GROUP BY hashtag_tid

This is assuming you have a table called weighting with 2 columns, one being the number of days previously and the other being the weighting for that number of days.这是假设您有一个名为 weighting 的表,其中包含 2 列,一个是之前的天数,另一个是该天数的权重。

Down side is the need to convert the timestamp to a date and then get the date difference on the join is not likely to be fast.不利的一面是需要将时间戳转换为日期然后在连接上获取日期差异不太可能很快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM