-1

这是来自足球系统,其中每场比赛、球员和比赛的“日志”都可以保存以供统计。

  • “bold_players”是玩家的表:id、name、age...
  • “bold_playlog”是包含所有游戏日志的表:id、createdate、logtype、playerid...
    “logtype = 1”是一个目标(只有一个目标——每个目标在表“bold_playlog”上都有一个帖子/id ”,所以如果一个游戏有四个目标,那么“bold_playlog”中就会有四个具有相同游戏ID的帖子)。

如何获得按目标排序的所有球员名单?

这是我现在的代码,但我想我必须加入这两个表或类似的东西!?:

$data_player = mysql_query("SELECT id, name, goals, playtogoal FROM bold_players ORDER BY name ASC ");
while ($row_player = mysql_fetch_assoc($data_player)) {

$seasonstart = mktime(0, 0, 0, 7, 1, date('Y')-1);

$logplayer = $row_player['id'];
$data_log = mysql_query("SELECT id, createdate, logtype FROM bold_playlog WHERE logplayer=$logplayer AND logtype = 1 AND createdate > $seasonstart ");
$getresult = mysql_num_rows($data_log);

if($getresult > 0) {

echo 'the result';

}}
4

2 回答 2

0

也许试试这个..我猜它一定是这样的

SELECT  name, goals, sum(playtogoal) as goals, createdate, logtype 
FROM bold_players as ps LEFT JOIN bold_playlog as pl ON ps.id=pl.logplayer
WHERE pl.logtype = 1 AND pl.createdate > $saesonstart
GROUP BY ps.id ORDER BY name ASC 

我会做一个 LEFT JOIN 因为可能有没有目标的球员,他们也会被展示出来

于 2013-07-09T06:31:23.813 回答
0

一个简单的LEFT JOIN应该做到这一点,这将首先订购最多的目标,这将是DESC一部分,ASC首先用于最少的目标。

SELECT a.*, COUNT(b.playerid) goals
FROM bold_players a
LEFT JOIN bold_log b
  ON b.playerid = a.id
 AND b.logtype=1
GROUP BY a.id, a.name, a.age
ORDER BY COUNT(b.playerid) DESC

一个用于测试的 SQLfiddle

如果您还需要更多日志类型,则必须稍微更改查询。如果offside是 logtype 2,查询将是;

SELECT a.*, 
  COALESCE(SUM(b.logtype=1), 0) goals, 
  COALESCE(SUM(b.logtype=2), 0) offsides
FROM bold_players a
LEFT JOIN bold_log b
  ON b.playerid = a.id
-- AND (b.logtype=1 OR b.logtype=2)      -- for speedup if there are many types
GROUP BY a.id, a.name, a.age
ORDER BY COALESCE(SUM(b.logtype=1), 0) DESC

另一个 SQLfiddle

于 2013-07-09T06:48:36.780 回答