我有一个足球台球网站。每周,我的朋友都会挑选每场比赛的获胜者。我想将每个球员的选秀权与其他球员进行比较,并列出相似的百分比。我发现这个页面可以帮助我计算特定一周的相似度:比较标签组以查找 PHP/MySQL 的相似度/分数。感谢 Ivar Bonsaksen,他的解决方案效果很好!
我现在要做的是显示过去几周每个玩家的累积相似度。
我有 3 个表要查询:配置文件 (spprofiles)、游戏 (sp6games) 和精选 (sp6picks)。另一个名为 Teams (sp6teams) 的表用于获取团队的名称,但在这里无关紧要。
Profiles (spprofiles)
+-----------+-------------+
| profileID | profilename |
+-----------+-------------+
| 52 | My Team A |
| 53 | Some Team B |
+-----------+-------------+
Games (sp6games)
+--------+--------+---------+------+
| gameID | weekID | visitor | home |
+--------+--------+---------+------+
| 1 | 2 | 9 | 21 |
| 2 | 2 | 14 | 6 |
| 17 | 3 | 6 | 9 |
| 18 | 3 | 30 | 21 |
+--------+--------+---------+------+
Picks (sp6picks)
+-----------+--------+------+
| profileID | gameID | pick |
+-----------+--------+------+
| 52 | 1 | 21 |
| 52 | 2 | 6 |
| 52 | 17 | 12 |
| 52 | 18 | 21 |
| 53 | 1 | 9 |
| 53 | 2 | 6 |
| 53 | 17 | 9 |
| 53 | 18 | 21 |
+-----------+--------+------+
本周的查询如下所示:
$weekID = 3; //the current weekID
$profile = 52; //the current ProfileID
SELECT
targetProfiles.profileID AS targetID,
sourceProfiles.profileID AS sourceID,
COUNT(targetProfiles.profileID)
/
(((SELECT COUNT(*) FROM sp6Picks LEFT JOIN sp6Games USING (gameID) WHERE profileID = sourceProfiles.profileID AND weekID = $weekID)
+
(SELECT COUNT(*) FROM sp6Picks LEFT JOIN sp6Games USING (gameID) WHERE profileID = targetProfiles.profileID AND weekID = $weekID))/2)
AS similarity
FROM
spProfiles AS sourceProfiles
LEFT JOIN
(SELECT sp6Picks.* FROM sp6Picks LEFT JOIN sp6Games USING (gameID) WHERE weekID = $weekID) AS sourcePicks
ON (sourcePicks.profileID = sourceProfiles.profileID)
INNER JOIN
(SELECT sp6Picks.* FROM sp6Picks LEFT JOIN sp6Games USING (gameID) WHERE weekID = $weekID) AS targetPicks
ON (sourcePicks.pick = targetPicks.pick AND sourcePicks.profileID != targetPicks.profileID)
LEFT JOIN
spProfiles AS targetProfiles
ON (targetPicks.profileID = targetProfiles.profileID)
WHERE sourceProfiles.profileID = $profile
GROUP BY targetID
如果我分别在几周内运行此查询,我会得到以下结果:
$weekID = 2;
+----------+----------+------------+
| targetID | sourceID | similarity |
+----------+----------+------------+
| 53 | 52 | 0.5000 |
+----------+----------+------------+
$weekID = 3;
+----------+----------+------------+
| targetID | sourceID | similarity |
+----------+----------+------------+
| 53 | 52 | 0.5000 |
+----------+----------+------------+
到目前为止,我为累积计算出的查询看起来像这样(但我尝试了其他几种变体)。基本上,我只是将 WHERE 子句更改为包括前几周weekID <= $weekID
,并将 Games 表添加到主 FROM 子句LEFT JOIN sp6games ON (targetPicks.gameID = sp6games.gameID)
中。
$weekID = 3; //the current weekID
$profile = 52; //the current ProfileID
SELECT
targetProfiles.profileID AS targetID,
sourceProfiles.profileID AS sourceID,
COUNT(targetProfiles.profileID)
/
(((SELECT COUNT(*) FROM sp6Picks LEFT JOIN sp6Games USING (gameID) WHERE profileID = sourceProfiles.profileID AND weekID <= $weekID)
+
(SELECT COUNT(*) FROM sp6Picks LEFT JOIN sp6Games USING (gameID) WHERE profileID = targetProfiles.profileID AND weekID <= $weekID))/2)
AS similarity
FROM
spProfiles AS sourceProfiles
LEFT JOIN
(SELECT sp6Picks.* FROM sp6Picks LEFT JOIN sp6Games USING (gameID) WHERE weekID <= $weekID) AS sourcePicks
ON (sourcePicks.profileID = sourceProfiles.profileID)
INNER JOIN
(SELECT sp6Picks.* FROM sp6Picks LEFT JOIN sp6Games USING (gameID) WHERE weekID <= $weekID) AS targetPicks
ON (sourcePicks.pick = targetPicks.pick AND sourcePicks.profileID != targetPicks.profileID)
LEFT JOIN
spProfiles AS targetProfiles
ON (targetPicks.profileID = targetProfiles.profileID)
LEFT JOIN sp6games ON (targetPicks.gameID = sp6games.gameID)
WHERE sourceProfiles.profileID = $profile
GROUP BY targetID, weekID
合并后的结果应该是 0.5000,但我得到的是:
$weekID = 3;
+----------+----------+------------+
| targetID | sourceID | similarity |
+----------+----------+------------+
| 53 | 52 | 0.7500 |
+----------+----------+------------+
问题是COUNT(targetProfiles.profileID)
在几周内没有正确总计,因此similarity
值被搞砸了。对于更大的数据集,它似乎也不是很有效。
感谢您花时间阅读,并可能提供帮助。