3

我目前有一个匹配结果表

+----------+----------+--------+-----------+--------+-----------+-----------+-----------+
| hometeam | awayteam | p1home | p2home... | p1away | p2away... | homescore | awayscore |
+----------+----------+--------+-----------+--------+-----------+-----------+-----------+

我用相当笨重的东西查询

SELECT
(SELECT COUNT(hometeam)
FROM fixture_data
WHERE (p1home = '$playerID' OR p2home = '$playerID' OR...)
AND hometeam = '$teamname')
+
(SELECT COUNT(awayteam)
FROM fixture_data
WHERE (p1home = '$playerID' OR p2home = '$playerID' OR...)
AND awayteam= '$teamname')
AS matches_played

获取特定球员 $playerID 为特定球队参加的比赛数 $teamname

桌上玩家很简单

+----------+-----------+-----------+
| playerID | firstname | surname   |
+----------+-----------+-----------+

打电话后的那一刻

SELECT * FROM players

我通过 PHP 为每个玩家调用初始查询,因为我正在努力将这两个查询连接到一个单独的 MYSQL 查询中以生成一个表

+----------+-----------+-----------+----------------+
| playerID | firstname | surname   | matches_played |
+----------+-----------+-----------+----------------+

这可能吗?还是我当前使用 PHP 的解决方案会尽可能高效?

4

3 回答 3

2

我建议将这些查询组合成一个:

SELECT COUNT(*) AS matches_played
FROM fixture_data
WHERE (p1home = '$playerID' OR p2home = '$playerID' OR...)
AND ( awayteam= '$teamname' OR hometeam = '$teamname')
于 2012-11-23T14:50:14.817 回答
2

如果您将球员列表从比赛表中取出并将其放入单独的球员、球队、比赛表格中,您可以让自己更轻松。那么你的总和将是加入三个并计算比赛的问题。球员是在家还是在客场,甚至有多少人参加了一场给定的比赛,都无关紧要。

于 2012-11-23T14:52:57.283 回答
1

这应该工作:

SELECT p.playerID, p.firstname, p.surname, COUNT(*)
  FROM players p, fisture_data f
 WHERE p.playerID='$playerID'
    AND (((p.playerID = f.p1home OR p.playerID=f.p2home...) AND f.hometeam='$teamname')
        OR ((p.playerID = f.p1away OR p.playerID=f.p2away...) AND f.awayteam='$teamname'))
GROUP BY p.playerID, p.firstname, p.surname

但是模型不是最优的,您应该创建一个新表来存储链接球员/比赛/球队。

于 2012-11-23T14:52:27.330 回答