Jump to content

PROBLEMS WITH "SELECT... AS" HELP


triphis

Recommended Posts

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
Share on other sites

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 10

Any other suggestions?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.