简体   繁体   English

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

[英]Inner join query to get result for each weapon

I have this query with an inner-join, however the WEAPON is unknown. 我有一个带有内部联接的查询,但是WEAPON是未知的。 I want to get the best killer for each weapon. 我想为每种武器争取最好的杀手。 How can I do this with this query? 如何使用此查询执行此操作? I'm not really experienced with advanced queries. 我对高级查询不是很了解。

    $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);

You need to specify the table names in the select clause since you are using join . 由于使用join ,因此需要在select子句中指定表名。 Try something like: 尝试类似:

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

I think you should group by weapon and not killer id, as you want number of killerID by weapon . 我想你应该按武器,而不是杀手ID,只要你想的数killerIDweapon

Note that you don't need all these backticks. 请注意,您不需要所有这些反引号。 Backticks are required only if your table name or coluumn name in the query is one of the reserved words. 仅当查询中的表名或列名是保留字之一时,才需要反引号。

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

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

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