avdzm Posted October 25, 2010 Share Posted October 25, 2010 Hey all, I am having a problem with this query, SELECT sum(tblRecipe.UNIT) as 'sumUnit', tblSpice.spiceID, spice, tblRecipe.UNIT as 'rUnit' , tblSpice.UNIT as 'sUnit', tblProduct.productID, product FROM ..... When I use the sum function sum(tblRecipe.UNIT) as 'sumUnit' as shown in the query above all I get is just the sum, and not the other fields. In phpadmin it works fine, except in my php page. I dont understand where the problem is coming from. Quote Link to comment https://forums.phpfreaks.com/topic/216760-a-problem-with-select-and-count-function-in-the-same-query/ Share on other sites More sharing options...
vichu.1985 Posted October 25, 2010 Share Posted October 25, 2010 Dude, Can u put the full query? Quote Link to comment https://forums.phpfreaks.com/topic/216760-a-problem-with-select-and-count-function-in-the-same-query/#findComment-1126134 Share on other sites More sharing options...
avdzm Posted October 25, 2010 Author Share Posted October 25, 2010 Here you go, $result = mysql_query("SELECT sum(tblRecipe.UNIT) as 'sumUnit', tblSpice.spiceID, spice, tblRecipe.UNIT as 'rUnit' , tblSpice.UNIT as 'sUnit', tblProduct.productID, product FROM tblRecipe, tblProduct, tblSpice WHERE tblRecipe.productID=tblProduct.productID AND tblRecipe.spiceID=tblSpice.spiceID AND tblProduct.productID='" . $productID . "';"); Quote Link to comment https://forums.phpfreaks.com/topic/216760-a-problem-with-select-and-count-function-in-the-same-query/#findComment-1126149 Share on other sites More sharing options...
vichu.1985 Posted October 25, 2010 Share Posted October 25, 2010 Dude, You try with this $result = mysql_query("SELECT sum(tblRecipe.UNIT) as 'sumUnit', tblSpice.spiceID, spice, tblRecipe.UNIT as 'rUnit' , tblSpice.UNIT as 'sUnit', tblProduct.productID, product FROM tblRecipe, tblProduct, tblSpice WHERE tblRecipe.productID=tblProduct.productID AND tblRecipe.spiceID=tblSpice.spiceID AND tblProduct.productID= '$productID'"); Quote Link to comment https://forums.phpfreaks.com/topic/216760-a-problem-with-select-and-count-function-in-the-same-query/#findComment-1126168 Share on other sites More sharing options...
avdzm Posted October 25, 2010 Author Share Posted October 25, 2010 didnt work Quote Link to comment https://forums.phpfreaks.com/topic/216760-a-problem-with-select-and-count-function-in-the-same-query/#findComment-1126256 Share on other sites More sharing options...
kai555 Posted October 26, 2010 Share Posted October 26, 2010 Hi when you use sum() you have and you select other columns you mysql doesn't know how to handle these other column with the sum eg Table test val | String 1 | test 1 | test 2 | test2 now selectting sum(val),String ..... now mysql doesn't know if you want it to return 4 or if you want it to return 2 for test and 2 for test2. So to solve this you have to group by the other columns select sum(val),String from table group by String this will give sum | String 2 | test 2 | test2 Hope this helped Quote Link to comment https://forums.phpfreaks.com/topic/216760-a-problem-with-select-and-count-function-in-the-same-query/#findComment-1126615 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.