0

大家好我已经编写了以下查询来组合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 评论:

我正在尝试提取 questiontitle,username,questionid from tblquestions以及基于来自的技术 IDreplycount and viewcount为该问题发布的最新回复QuestionIDtblreplys

4

2 回答 2

0

您的查询基本上是这样的:

SELECT
    blah
FROM
    (
        (SELECT
            blah
        FROM
            Questions
            INNER JOIN Replies ON QuestionID AND TechID) tblr1
        RIGHT JOIN Questions tblr2 ON QuestionID
    ) tblr3
    LEFT JOIN Technologies ON tblr3.tblr1.QuestionID = Technologies.QuestionID

我知道这不是一个有效的查询,但请耐心等待。tblr1包含INNER JOIN问题和答案之间。然而,由于没有问题的答案,salary这些记录是NULL针对salary问题的。然后RIGHT JOINto Questions 介绍了该salary问题,并且该问题存在于tblr3结果集中,因为它是一个RIGHT JOIN即使tblr1对于该问题为空的问题。

then to 技术是LEFT JOIN基于inside which is really the from 。通过查看子查询的列列表,如果您更改为这将确保未回答的问题加入到表中QuestionIDtblr3QuestionIDtblr1tblr3tblr1.QuestionIDtblr2.QuestionIDTechnology

编辑:我认为以下查询将为您提供结果并且更简单:

WITH LatestReplies AS
(
  SELECT
    ReplyID,
    QuestionID,
    ReplyDate,
    UserName,
    Reply
  FROM
    (SELECT
      ReplyID,
      QuestionID,
      ReplyDate,
      UserName,
      Reply,
      ROW_NUMBER() OVER (PARTITION BY QuestionID ORDER BY ReplyDate DESC) ReplyOrder
    FROM
      tblReplies) A
  WHERE
    ReplyOrder = 1
)
SELECT
  tblQuestions.DatePosted,
  tblQuestions.QuestionID,
  tblTechnology.TechnologyName,
  tblQuestions.TechID,
  tblQuestions.QuestionTitle,
  tblQuestions.UserName,
  tblQuestions.ReplyCount,
  tblQuestions.ViewCount,
  LatestReplies.ReplyDate,
  LatestReplies.UserName RepliedName
FROM
  tblQuestions
  LEFT JOIN tblTechnology ON tblQuestions.TechID = tblTechnology.TechID
  LEFT JOIN LatestReplies ON tblQuestions.QuestionID = LatestReplies.QuestionID
于 2013-02-12T14:50:34.873 回答
0

毕竟解决了以下问题

select 
    FI.QuestionID,
    FI.QuestionTitle,
    FI.UserName,
    FI.DatePosted,
    FI.[date],
    FI.RepliedName,
    FI.viewCount,
    FI.ReplyCount,
    FI.ReplyMsg,
    TT.TechID,
    TT.TechName 
from 
    tblTechnology TT,
    (select distinct
        TQ.TechID,
        TQ.QuestionID,
        TQ.QuestionTitle,
        TQ.UserName,
        TQ.DatePosted,
        TR.[date],
        TR.UserName as RepliedName,
        TQ.viewCount,
        TQ.ReplyCount,
        TR.ReplyMsg 
    from  
        tblQuestions TQ 
        LEFT OUTER JOIN tblReplies TR ON 
                TR.TechID=TQ.TechID 
            and TR.QuestionID = TQ.QUESTIONID 
            and TR.[date] in (select 
                                MAX(TR.[date]) 
                            from 
                                tblReplies TR 
                            group by 
                                TR.QuestionID)
    ) FI 
where 
    FI.TechID=TT.TechID 
and TT.TechID=@TechID

在此处输入图像描述

于 2013-02-13T07:40:34.497 回答