Jump to content

If statements in mysql?


EchoFool

Recommended Posts

If there a way to add if statements to mysql queries?

 

I want to add if statements to the ORDER BY clause .. heres an example of my idea:

 

ORDER BY (

Listing.Ratings  * number of rows with the value of one +

Listing.Ratings  * number of rows with the value of two +

Listing.Ratings  * number of rows with the value of three +

Listing.Ratings  * number of rows with the value of four +

Listing.Ratings  * number of rows with the value of five

) AS TotalScore DESC

 

Obviously this is not in MYSQL syntax but is this possible? My current quiery is this:

 

<?php
$SELECT = mysql_querY("SELECT listings.*
FROM listings 
INNER JOIN listratings ON
	listings.GameID=listratings.GameID
WHERE listings.Authorised='1'")
Or die(mysql_error());
?>

 

Any idea how i can add such a complex ORDER BY idea?

Link to comment
Share on other sites

Well im trying to use a more accurate AVG() by using maths involved.... so say you got a table:

 

ID |  Voted  |

1  |    4    |

1  |    5    |

1  |    5    |

2  |    5    |

 

 

What i want to do is order by sum of (Totalrows multiplied by VotedFor that equals 1) + (totalrows multiplied by VotedFor that equals 2) ( up to 5) AS TotalScore

Then use that total score in an ORDER CLAUSE

 

In the above table the result would be:

 

(5 * 2) + (1 * 4) = 14 for ID 1

(1 * 5) = 5 for ID 2

 

ORDER BY DESC

 

ID 1 = 1st

ID 2 = 2nd.

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.