开发者

sql query to count when question_id is same and user_id is different

I have following table:

 res_id  quiz_id     sender_id   user_id     question_id   points
  1         1           2      33       20      0
  2         1           2      34       20      0   
  3         1           2      24       20      0
  4         2           3      22       2       1
  5         3           4      3        6       1

I want to get result if quiz_id is same for three rows and question_id is same and sender_id is same make sure user_id is different and points are 0. If this condition is satisfied pass value of 1 to the php variable here is example:

res_id  quiz_id     sender_id   user_id     question_id   points
  1         1           2      33       20      0
  2         1           2      34       20      0   
  3       开发者_如何学编程  1           2      24       20      0


The "If this condition is satisfied pass value of 1 to the php variable" isn't quite clear to me (do you just want to know is there three (or more) rows which satisfy your query?) but the query should be something like

SELECT 
  count(*)
FROM tab t1
  JOIN tab t2 ON(t1.quiz_id = t2.quiz_id)AND(t1.question_id = t2.question_id)AND(t1.sender_id = t2.sender_id)
  JOIN tab t3 ON(t1.quiz_id = t3.quiz_id)AND(t1.question_id = t3.question_id)AND(t1.sender_id = t3.sender_id)
WHERE(t1.user_id <> t2.user_id)AND(t2.user_id <> t3.user_id)AND(t1.user_id <> t3.user_id)
      AND(t1.points = 0)AND(t2.points = 0)AND(t3.points = 0)
GROUP BY t1.quiz_id, t1.sender_id, t1.question_id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜