用sql子查询进行sql查询

时间:2013-02-12 14:08:02

标签: sql sql-server-2008 tsql

大家好我编写了以下查询来合并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

enter image description here

代替NULL我需要提取tblQuestions中存在的一些数据,也可以减少查询。

来自OPs评论:

我正在尝试提取q uestiontitle,username,questionid from tblquestions以及replycount and viewcount以及根据QuestionIDtblreplys <的技术ID为这些问题发布的最新回复< / p>

2 个答案:

答案 0 :(得分: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 JOIN会引入salary问题以及tblr3结果集中存在的问题,因为它是RIGHT JOIN,即使tblr1对此问题为空

LEFT JOIN然后是技术基于QuestionID中的tblr3QuestionID来自tblr1。通过查看tblr3子查询的列列表,如果您将tblr1.QuestionID更改为tblr2.QuestionID,这将确保未答复的问题加入Technology

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

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

答案 1 :(得分: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

enter image description here