I’m trying to select a record with the most effective votes. Each record has an id, the number of upvotes (int) and the number of downvotes (int) in a MySQL database.
I know basic update, select, insert queries but I’m unsure of how to form a query that looks something like:
SELECT *
FROM topics
WHERE MAX(topic.upvotes - topic.downvotes)
You are not using MAX() right.
Here is pretty fast query:
To run an update:
I just ran that on a table with 250,000 records (it’s very similar – inventory usage – looking for a most popular part) and it took .203 of a second – on my dev machine – this is not even production server ( where it tppl 0.016 of a second)
UPDATE:
Yeah I didn’t think about that possibility that you might have more than one top results.
popular_ids – will contain popular records as a text field, that can be easily parsed out if you need to.