Consider the following “users” table:
uid followers following ff_fraction
--------------------------------------
1 40 2 20
2 10 3 3
3 0 2 0
4 5 0 5
5 77 2
6 9 3
I want to calculate, for each user, the fraction of followers/following, rounded to a whole number. I filled in the table for the first 4 users:
- 40/2 = 20
- 10/3 = 3.3333 => rounded 3
- 0/2 = 0
- 5/0 => this would give an error, so I counted +1, making 5/1 = 5
I now have the following query, but it does not account for “following” being zero (and that it thus would have to add 1 in order to not get a devision by zero error):
UPDATE users SET ff_fraction = ROUND(followers/following)
Is it possible to solve this in a MySQL query?
You can do the following: