我正在做一个Q &一个网站,类似于这个网站和雅虎的答案。我有3个表格 - smf_members,qa_questions和qa_answers。插入SQL连接查询
在此查询中,我想从qa_questions中选择一些字段,smf_members的几个字段以及用于question_id的ga_answers中的记录数。这样我就可以得到关于这个问题的一些基本信息,关于该成员的一些基本信息以及答案的数量。
这是我迄今为止制作的查询,它几乎可以工作,但不会返回没有答案的问题(即此答案的答案表中没有记录)。
SELECT qa_questions.question_id,
qa_questions.question_title,
qa_questions.question_content,
qa_questions.time_asked,
qa_questions.question_author,
qa_questions.votes,
qa_questions.views,
qa_questions.pretty_url,
smf_members.real_name,
smf_members.id_member,
COUNT(qa_answers.question_id) AS answers
FROM qa_questions,
qa_answers,
smf_members
WHERE qa_questions.deleted = 0
AND smf_members.id_member = qa_questions.question_author
AND qa_answers.question_id = qa_questions.question_id
ORDER BY qa_questions.time_asked DESC
LIMIT 10
相关:http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html – 2010-09-09 21:04:48