How can i write this query in MySql? -
i have table:
    question_id | user_id | answer | weight         213     |   22    |   25   |   50         213     |   106   |   75   |   50         216     |   22    |   100  |   50         216     |   106   |   0    |   50       i want write mysql query calculate, 2 specified user_id's: 
user1's weight * (user1's answer - user2's answer)
and sum values question_id's. so, example above, if user1's id 22 , user2's id 106: 
50*(25-75)+50*(100-0)=2500
you can self join:
select sum(u1.weight * (u1.answer - u2.answer)) total table u1 join table u2 on u2.question_id = u1.question_id u1.user_id = 22 , u2.user_id = 106   make sure column types not "unsigned", otherwise, you'll need cast values signed.
Comments
Post a Comment