John John - 4 days ago 5
MySQL Question

MySQL order by not ordering

I got items rated by both ratings, and use an ORDER BY key to order them by positive, but with an example table:

//(item)-(pos)-(neg):
item 1 - 50 - 200
item 2 - 24 - 0
item 3 - 15 - 12


But item 1 is really less rating than the others, how would I fix this?

Answer

You can do ORDER BY pos - neg DESC but it will be slow because it cannot use an index.

You can denormalize your database by explicitly storing the value of pos - neg in the table so that you can index it to improve performance. Usually denormalization is regarded as a bad thing and should be avoided but if it the improved performance is required then it is an acceptable approach.

Comments