我的联接查询出了点问题,或者我需要GROUP
table questions
-------------------------------------------
| id | questions_id1| category |
-------------------------------------------
| 1 | p1 | 1 |
| 2 | p2 | 2,1 |
| 3 | p3 | 1 |
| 4 | p4 | 2,1 |
| 5 | p5 | 2,1 |
------------------------------------------
table answers
-----------------------------------------
| id | questions_id2 | user_id |
-----------------------------------------
| 1 | p1 | u1 |
| 2 | p1 | u2 |
| 3 | p2 | u1 |
| 4 | p2 | u2 |
| 5 | p3 | u1 |
| 6 | p4 | u2 |
-----------------------------------------
table extra_category
---------------------------------------------
| id | questions_id3 |extra_category |
---------------------------------------------
| 1 | p1 | 1 |
| 2 | p2 | 1 |
| 3 | p3 | 1 |
| 4 | p4 | 1 |
| 5 | p5 | 1 |
---------------------------------------------
SELECT questions.*, answers.*, extra_category.* FROM extra_category
LEFT JOIN questions ON extra_category.questions_id3 = questions.questions_id1
LEFT JOIN answers ON extra_category.questions_id3 = answers.questions_id2
WHERE extra_category = 1 AND category LIKE '%1%'
order by case when user_id = '1' then 1 else 0 end, a.id ASC
预期结果:对于user_id = 1
----------------------------------------------------------------------------------------
| id | question_id1 | question_id2 | user_id | category | extra_category |
----------------------------------------------------------------------------------------
| 4 | p4 | p4 | u2 | 1 | 1 |
| 5 | p5 | p5 | null | 1 | 1 |
| 1 | p1 | p1 | u1 | 1 | 1 |
| 2 | p2 | p2 | u1 | 1 | 1 |
| 3 | p3 | p3 | u1 | 1 | 1 |
---------------------------------------------------------------------------------------
预期结果:对于user_id = 2
------------------------------------------------------------------------------------
| id |question_id1| question_id2| user_id | category | extra_category |
-------------------------------------------------------------------|----------------|
| 3 | p3 | p3 | u1 | 1 | 1 |
| 5 | p5 | p5 | null | 1 | 1 |
| 1 | p1 | p1 | u2 | 1 | 1 |
| 2 | p2 | p2 | u2 | 1 | 1 |
| 4 | p4 | p4 | u2 | 1 | 1 |
-------------------------------------------------------------------------------------
现在为user_1 = 1得出的结果
-----------------------------------------------------------------------------------
| id |question_id1| question_id2| user_id | category | extra_category |
-----------------------------------------------------------------------------------
| 1 | p1 | p1 | u2 | 1 | 1 |
| 2 | p2 | p2 | u2 | 1 | 1 |
| 4 | p4 | p4 | u2 | 1 | 1 |
| 5 | p5 | p5 | null | 1 | 1 |
| 1 | p1 | p1 | u1 | 1 | 1 |
| 2 | p2 | p2 | u1 | 1 | 1 |
| 3 | p3 | p3 | u1 | 1 | 1 |
------------------------------------------------------------------------------------
测验有100个问题。每个用户都收到相同的问题。答案保存在表b中。在下一次:他尝试解决测试时,将在最后显示已经正确回答的答案。还有100个问题。
答案 0 :(得分:1)
SELECT questions.*, answers.*, extra_category.* FROM extra_category
LEFT JOIN questions ON extra_category.questions_id3 = questions.questions_id1
LEFT JOIN answers ON extra_category.questions_id3 = answers.questions_id2 and answers.user_id = 1
WHERE extra_category = 1 AND category LIKE '%1%'
order by case when user_id = '1' then 1 else 0 end, a.id ASC
工作正常