我有两张桌子:questions
和questions_lookup
。如果这是一个好问题,用户投票决定是否将其放在网站上。
table: questions
id
question
date_created
table: questions_lookup
id
question_id // (id linked to questions table)
user_id // (the id of the user, I store this in a session variable called $me)
show // (1 or 0, show or don't show)
我想要一个 php 页面,它从按 date_created 排序的问题表中提取所有问题,然后显示用户是否已回答。当我尝试进行任何连接时,我最终会显示重复的问题,因为它会提取其他用户的答案。
所以如果有10个问题。一个特定的用户只回答了 3 个问题。我们仍然显示所有 10 个问题,但标记他们已回答的问题。
所以我基本上想显示如下内容:
Question 1
Question 2 (answered)
Question 3 (answered)
Question 4
Question 5
Question 6
Question 7 (answered)
Question 8
Question 9
Question 10
我试过了:
SELECT * FROM questions
RIGHT JOIN questions_lookup
ON (questions.id = questions_lookup.question_id)
WHERE questions_lookup.user_id = '$me'
ORDER BY questions.date_created DESC