内部联接查询以获取每个武器的结果

时间:2014-04-04 16:06:20

标签: php mysql sorting inner-join

我有一个内部联接的查询,但是WEAPON是未知的。 我想为每种武器获得最好的杀手。如何使用此查询执行此操作?我对高级查询并不熟悉。

    $q = $mysql->query("SELECT `killerID`
            , COUNT(`killerID`) AS tot_kills
            , MIN(`Username`) AS username
            FROM `kills`
            INNER JOIN `players`
            ON `players`.`id` = `kills`.`killerid`
            WHERE `killText` LIKE '%###WEAPON###%'
            GROUP BY `killerID`
            ORDER BY `tot_kills` DESC") or die($mysql->error);

2 个答案:

答案 0 :(得分:0)

您需要在select子句中指定表名,因为您使用的是join。 尝试类似:

SELECT `kills`.`killerID`
            , COUNT(`kills`.`killerID`) AS tot_kills
            , MIN(`players`.`Username`) AS username
            FROM `kills`
            INNER JOIN `players`
            ON `players`.`id` = `kills`.`killerid`
            WHERE `kills`.`killText` LIKE '%###WEAPON###%'
            GROUP BY `kills`.`killerID`
            ORDER BY `kills`.`tot_kills` DESC

答案 1 :(得分:0)

我认为您应该按武器而不是杀手ID进行分组,因为您希望killerID的数量为weapon

请注意,您不需要所有这些反引号。仅当查询中的表名或coluumn名称是保留字之一时才需要反引号。

SELECT killerID
        , COUNT(killerID) AS tot_kills
        , MIN(Username) AS username
FROM kills
INNER JOIN players
  ON players.id = kills.killerid
WHERE killText LIKE '%###WEAPON###%'
GROUP BY kills.killText
ORDER BY tot_kills DESC