"IF" statement MySQL -
"IF" statement MySQL -
i making app can vote things, have table contains "rating" goes 0 100 , contains "votes" too, represents number of votes. using:
update maincategory set rating='75'/votes
but when nobody else have voted yet, votes "0". , makes "75/0" (division "0"). how can create "if" statement or else verifies if thing have "0" votes yet , not split "votes" (because they're "0")?
thanks in advance
edit: vote logic doesn't create much sense, have seen now... sorry, must create right this? maybe add together ratings , split number of votes?
just run on rows have votes.
update maincategory set rating='75'/votes votes>0
leave others @ default of 0
or null
rating , handle in app.
also, '75'/votes
bit sounds bit off me. 1 vote, it'll have rating of 75. 2 votes, it'll have rating of 37.5. votes typically wouldn't decrease rating.
typically, new rating set (this pseudocode, not valid sql):
rating = ((current_rating * current_vote_count) + new_rating) / (current_vote_count + 1)
mysql if-statement division rating
Comments
Post a Comment