0

So I am trying to find the amount of kills in the league database during the last 10 matches with a character. Both are set up correctly to work and without the SUM and LIMIT & ORDER it works perfectly. However, when attempting to find the SUM of the last 10 rows added I seem to be hitting a stumbling block. After trying a few suggested things from different sites I have come up with something which seems as though it should work although it brings up the error:

Warning: mysql_result() expects parameter 1 to be resource, boolean given in "filepath" on the following line:

$kills = mysql_result(mysql_query("
    SELECT SUM(kills) 
    FROM (SELECT `kills` 
        FROM league 
        WHERE `gameType`='N' AND `champion`='$champion' 
        ORDER BY `matchID` DESC 
        LIMIT 10)"), 0);

The error comes when I try to echo out $kills

Any help would be greatly appreciated.

Lkopo
  • 4,730
  • 8
  • 33
  • 60

1 Answers1

0

When you use a subquery in the FROM clause, MySQL requires you to give it an alias.

$kills = mysql_result(mysql_query("
    SELECT SUM(kills) 
    FROM (SELECT `kills` 
        FROM league 
        WHERE `gameType`='N' AND `champion`='$champion' 
        ORDER BY `matchID` DESC 
        LIMIT 10) AS x"), 0);

You would have realized this was the error if you had bothered to do echo mysql_error(); when the query failed.

The query you actually want is:

SELECT SUM(kills)
FROM (SELECT *
    FROM league
    WHERE gameType = 'N'
    ORDER BY matchID DESC
    LIMIT 10) x
WHERE champion = '$champion'
Barmar
  • 669,327
  • 51
  • 454
  • 560
  • I apologise, I haven't used SQL for quite a while. And how would I go about doing something like finding the COUNT of result WHERE it is W in the last 10 rows? – user2882574 Sep 13 '14 at 21:30
  • I've added what I think your query should be. – Barmar Sep 13 '14 at 21:37