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 Quote 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. Quote 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()); ?> Quote 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" Quote Link to comment https://forums.phpfreaks.com/topic/98648-sum/#findComment-504999 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.