triphis Posted August 19, 2003 Share Posted August 19, 2003 Im trying to return an average from a MySQL query. Here is my current query: SELECT id,name,points,votes,(points/votes) AS average FROM ratings WHERE id=$id ORDER BY average DESC; What is the correct way (if any) to to mathematical equations in a query? Thank you for your help! Link to comment https://forums.phpfreaks.com/topic/916-problems-with-select-as-help/ Share on other sites More sharing options...
DylanBlitz Posted August 19, 2003 Share Posted August 19, 2003 I don\'t think you can do mathematics inside of the select. I think you need to pull the data out and do it with the variables. Link to comment https://forums.phpfreaks.com/topic/916-problems-with-select-as-help/#findComment-3052 Share on other sites More sharing options...
triphis Posted August 19, 2003 Author Share Posted August 19, 2003 Could someone give me an example? If I do it with variables, how can I order them BY the average? Link to comment https://forums.phpfreaks.com/topic/916-problems-with-select-as-help/#findComment-3053 Share on other sites More sharing options...
DylanBlitz Posted August 19, 2003 Share Posted August 19, 2003 *sigh* I must be tired lol, you can do mathematics, give this a try, just get rid of the () SELECT id,name,points,votes, points/votes AS average FROM ratings WHERE id=$id ORDER BY average DESC; Link to comment https://forums.phpfreaks.com/topic/916-problems-with-select-as-help/#findComment-3054 Share on other sites More sharing options...
triphis Posted August 19, 2003 Author Share Posted August 19, 2003 Awsome! thank you, it worked. However, I have another question: If, say, for ID #3, votes=0, and points=0, the average will not even show up (you cant divide 0 by 0). For some reason, id #3 is placed ABOVE other IDs with actual averages. Ive tried both ASC, and DESC and the \"empty\" values do not move, only the IDs with actual averages, beneath them move. For example: DESC ID Average 3 --- 0 2 --- 0 4 --- 5.2 1 --- 4.7 ASC ID Average 3 --- 0 2 --- 0 1 --- 4.7 4 --- 5.2 Is there anyway to fix this? This doesn\'t work (because the empty values are NOT 0, they simply do not exist) SELECT id, name, points, votes, points/votes AS average WHERE average > 0 FROM ratings ORDER BY average DESC LIMIT 10Any other suggestions? Link to comment https://forums.phpfreaks.com/topic/916-problems-with-select-as-help/#findComment-3055 Share on other sites More sharing options...
Barand Posted August 19, 2003 Share Posted August 19, 2003 Filter out those with no votes to avoid the division by zero SELECT id, name, points, votes, points/votes AS average FROM ratings WHERE votes > 0 ORDER BY average DESC LIMIT 10 Link to comment https://forums.phpfreaks.com/topic/916-problems-with-select-as-help/#findComment-3059 Share on other sites More sharing options...
DylanBlitz Posted August 19, 2003 Share Posted August 19, 2003 Yup, like barand said, exclude the ones that don\'t have any votes, with your code it would be like SELECT id,name,points,votes, points/votes AS average FROM ratings WHERE (id=$id && votes > 0) ORDER BY average DESC; Link to comment https://forums.phpfreaks.com/topic/916-problems-with-select-as-help/#findComment-3060 Share on other sites More sharing options...
triphis Posted August 20, 2003 Author Share Posted August 20, 2003 Awsome! Thank you Barand and DylanBlitz! You have both been incredably helpful! ^^ Link to comment https://forums.phpfreaks.com/topic/916-problems-with-select-as-help/#findComment-3064 Share on other sites More sharing options...
DylanBlitz Posted August 20, 2003 Share Posted August 20, 2003 no problem, glad we could help out Link to comment https://forums.phpfreaks.com/topic/916-problems-with-select-as-help/#findComment-3070 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.