maxudaskin Posted March 30, 2008 Share Posted March 30, 2008 Is it possible to use SUM() and WHERE in the same query if they are both using the same column? <?php $query = mysql_query("SELECT type, pid, SUM(totaltime) as sumValue FROM pireps GROUP BY `pid` WHERE type = '0' AND `pid` = '$poster_pid'"); $hours = mysql_fetch_array($query) or die ("MySQL Error on line 472: " . mysql_error()); // Line 472 ?> Outputs Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/.grable/vzoom/virtualzoom.net/comm/forum.php on line 473 MySQL Error on line 472: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE type = '0' AND `pid` = '0100'' at line 1 Link to comment https://forums.phpfreaks.com/topic/98648-sum/ Share on other sites More sharing options...
BlueSkyIS Posted March 30, 2008 Share Posted March 30, 2008 your problem is using a GROUP BY before the WHERE. GROUP BY clause should come after WHERE clause. Link to comment https://forums.phpfreaks.com/topic/98648-sum/#findComment-504980 Share on other sites More sharing options...
redarrow Posted March 30, 2008 Share Posted March 30, 2008 <?php $query = mysql_query("SELECT type, pid, SUM(totaltime) as sumValue FROM pireps WHERE type = '0' AND `pid` = '$poster_pid' AND GROUP BY `pid`"); $hours = mysql_fetch_array($query) or die ("MySQL Error on line 472: " . mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/98648-sum/#findComment-504997 Share on other sites More sharing options...
BlueSkyIS Posted March 30, 2008 Share Posted March 30, 2008 ^ You'll be seeing the mysql_error() on that one. No AND before a GROUP clause. my preference: "SELECT type, pid, SUM(totaltime) as sumValue FROM pireps WHERE type = '0' AND pid = '$poster_pid' GROUP BY pid" Link to comment https://forums.phpfreaks.com/topic/98648-sum/#findComment-504999 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.