This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result
                                
                                    (32个答案)
                                
                        
                                5年前关闭。
            
                    
因此,我试图在最近10个有角色的比赛中找到联盟数据库中的杀死人数。两者均已正确设置为可以正常工作,并且没有SUM和LIMIT&ORDER即可完美运行。但是,当尝试查找添加的最后10行的总和时,我似乎遇到了绊脚石。在尝试了来自不同站点的一些建议事项后,我想到了一些似乎可以工作的东西,尽管它带来了错误:

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


当我尝试回显$ kills时出现错误

任何帮助将不胜感激。

最佳答案

FROM子句中使用子查询时,MySQL要求您为其赋予别名。

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


如果在查询失败时不愿意执行echo mysql_error();,您将意识到这是错误。

您实际想要的查询是:

SELECT SUM(kills)
FROM (SELECT *
    FROM league
    WHERE gameType = 'N'
    ORDER BY matchID DESC
    LIMIT 10) x
WHERE champion = '$champion'

关于php - 选择最后x行的总和SQL和PHP ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25827799/

10-11 02:50