こんにちは、私は次のクエリを作成して、3 つのテーブルの結果を結合し (tblQuestions,tblReplies,tblTechnology
必要な結果を次のように表示します)
SELECT tech.DatePosted,
tech.QuestionID,
tech.TechnologyName,
tblr3.TechID,
tblr3.QuestionTitle,
tblr3.UserName,
tblr3.ReplyCount,
tblr3.viewCount,
tblr3.date,
tblr3.RepliedName
FROM (SELECT tblr1.DatePosted,
tblr1.QuestionID,
tblr2.TechID,
tblr2.QuestionTitle,
tblr2.UserName,
tblr2.ReplyCount,
tblr2.viewCount,
tblr1.date,
tblr1.RepliedName
FROM (SELECT tblq.DatePosted,
tblq.TechID,
tblq.QuestionID,
tblq.RepliedName,
tblq.QuestionTitle,
tblq.UserName,
tblq.ReplyCount,
tblq.viewCount,
tblq.date
FROM (SELECT q.DatePosted,
q.TechID,
q.QuestionID,
q.QuestionTitle,
q.UserName,
q.ReplyCount,
q.viewCount,
r.date,
r.UserName AS RepliedName
FROM tblQuestions AS q, tblReplies AS r
WHERE r.QuestionID = q.QuestionID
AND r.TechID = q.TechID
AND q.TechID = 1) AS tblq,
(SELECT r.QuestionID,
max(r.date) AS PostedDate
FROM tblReplies AS r
GROUP BY QuestionID) AS tblr
WHERE tblq.QuestionID = tblr.QuestionID
AND tblq.date = tblr.PostedDate) AS tblr1
RIGHT OUTER JOIN
(SELECT q.QuestionID,
q.TechID,
q.UserName,
q.viewCount,
q.ReplyCount,
q.QuestionTitle
FROM tblQuestions AS q
WHERE q.TechID = 1) AS tblr2
ON tblr2.QuestionID = tblr1.QuestionID) AS tblr3
LEFT OUTER JOIN
(SELECT q.QuestionID,
q.DatePosted,
t.TechID,
t.TechName AS TechnologyName
FROM tblTechnology AS t, tblQuestions AS q
WHERE q.TechID = t.TechID) AS tech
ON tblr3.TechID = tech.TechID
AND tblr3.QuestionID = tech.QuestionID
AND tech.TechID = 1
これにより、期待どおりに次の結果が得NULL
られますが、データが存在するいくつかのフィールドに到達します
NULL
に存在するデータを引き出す必要がある代わりにtblQuestions
、クエリを減らすこともできます。
OPコメントより:
テクノロジー IDに基づいて、その質問に対して投稿された最新の回答uestiontitle,username,questionid from tblquestions
とともに、q を引き出そうとしています。replycount and viewcount
QuestionID
tblreplys