简体   繁体   中英

Inner join query to get result for each weapon

I have this query with an inner-join, however the WEAPON is unknown. 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 . 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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