我有以下表格:
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
我想获得结果如果quiz_id对于三行是相同的并且question_id是相同的并且sender_id是相同的,则确保user_id不同并且点为0.如果满足此条件,则将1的传递值传递给php变量示例:
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
答案 0 :(得分:1)
“如果满足这个条件,则传递值为1到php变量”对我来说不是很清楚(你只是想知道有三行(或更多)行满足你的查询吗?)但查询应该像
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