I do not have much knowledge of mysql. My django model is as follows:
class Exercise_state(models.Model):
exercise = models.ForeignKey(Exercise, blank=True, null=True)
user = models.ForeignKey(User, blank=True, null=True)
intensity_level = models.IntegerField(default='1')
progress = models.IntegerField(default='0')
current_date = models.DateField(auto_now=True)
user_rating = models.IntegerField(default='0')
I want to retrieve count of user_id's which satisfy exercise_id=1 corresponding intensity_level=7 and so on for all exercise_id's till 7. In short, users who have reached intensity_level=7 in all the exercises.
I have written one query which is as follows:
select count(user_id) from demo_exercise_state where
exercise_id=1 and intensity_level=7 and
exercise_id=2 and intensity_level=7 and
exercise_id=3 and intensity_level=7 and
exercise_id=4 and intensity_level=7 and
exercise_id=5 and intensity_level=7 and
exercise_id=6 and intensity_level=7 and
exercise_id=7 and intensity_level=7;
As far as I can cross check results from my database directly no user has yet completed his training (i.e. who has reached intensity_level=7 in all exercise types), so it returns the count as '0'.
I am sure this can query be optimized as there is quite much repeatation, but I am not sure how.
Also I want to execute the same (i.e. get the same result) in my django view. There I have something like
all_exercise_finish_count = Exercise_state.objects.filter(exercise_id=1, intensity_level=7).count()
How can I reflect the same in this django view?