我的数据库中有三个表用于提问,答案和查看:
问题:
---------------------------------
| ID | category | title |
---------------------------------
| 1 | 1 | What is this? |
---------------------------------
| 2 | 1 | Who is this? |
---------------------------------
依此类推:category是来自不同表的类别ID,但这并不重要。
然后我有答案:
-------------------------------------------
| ID | question | answer | user | date |
-------------------------------------------
| 1 | 1 | Blue | 25 | 1.1.15 |
-------------------------------------------
| 2 | 1 | Red | 18 | 2.2.15 |
-------------------------------------------
| 3 | 2 | Brad | 25 | 1.1.15 |
-------------------------------------------
问题表中的问题是user,用户是user_id
然后我看了:
-----------------------------------
| ID | category | question | user |
-----------------------------------
| 1 | 1 | 1 | 25 |
-----------------------------------
| 2 | 1 | 1 | 18 |
-----------------------------------
| 3 | 1 | 2 | 25 |
-----------------------------------
这将跟踪用户看到的问题,因此他们两次看不到相同的问题。关键是用户可以给出另一个用户给出的答案,这样就不会存储在答案表中。
我尝试过这个MYSQL代码:
SELECT Q.ID, Q.category, Q.title, A.ID, A.answer
FROM questions Q, answers A
WHERE
NOT EXISTS (
SELECT *
FROM category_answered S
WHERE Q.ID = S.question AND S.user = '$user' AND S.category = '$category'
)
AND
CASE
WHEN Q.category = '$category' THEN Q.ID = A.question
ELSE Q.category = '$category'
END
ORDER BY RAND() LIMIT 1
此列出除匹配问题外的所有内容。所以ORDER BY RAND() LIMIT 1
无效。
欢迎任何帮助