【问题标题】:SQL query inner joinSQL 查询内连接
【发布时间】:2011-04-11 21:04:56
【问题描述】:

我想修改我的论坛代码中的查询,以从表 tblProfile 中返回两个字段。该表有两个字段,“EmailAddress”和“EmailVerified”。

这是原始查询:

SELECT * 
FROM   (SELECT TOP 10 [Scirra].[dbo].[tblForumThread].thread_id, 
                      [Scirra].[dbo].[tblForumThread].MESSAGE, 
                      [Scirra].[dbo].[tblForumThread].message_date, 
                      [Scirra].[dbo].[tblForumThread].show_signature, 
                      [Scirra].[dbo].[tblForumThread].ip_addr, 
                      [Scirra].[dbo].[tblForumThread].hide, 
                      [Scirra].[dbo].[tblForumAuthor].author_id, 
                      [Scirra].[dbo].[tblForumAuthor].username, 
                      [Scirra].[dbo].[tblForumAuthor].homepage, 
                      [Scirra].[dbo].[tblForumAuthor].location, 
                      [Scirra].[dbo].[tblForumAuthor].no_of_posts, 
                      [Scirra].[dbo].[tblForumAuthor].join_date, 
                      [Scirra].[dbo].[tblForumAuthor].SIGNATURE, 
                      [Scirra].[dbo].[tblForumAuthor].active, 
                      [Scirra].[dbo].[tblForumAuthor].avatar, 
                      [Scirra].[dbo].[tblForumAuthor].avatar_title, 
                      [Scirra].[dbo].[tblForumGroup].name 
                             AS groupname, 
                      [Scirra].[dbo].[tblForumGroup].stars, 
                      [Scirra].[dbo].[tblForumGroup].custom_stars, 
                      [Scirra].[dbo].[tblForumGuestName].name 
                             AS guestname, 
                      Row_number() OVER (ORDER BY 
                             [Scirra].[dbo].[tblForumThread].message_date ASC) 
                      AS 
                      rownum
        FROM   ([Scirra].[dbo].[tblForumGroup] 
                INNER JOIN ([Scirra].[dbo].[tblForumAuthor] 
                INNER JOIN [Scirra].[dbo].[tblForumThread] 

                ON [Scirra].[dbo].[tblForumAuthor].author_id = [Scirra].[dbo].[tblForumThread].author_id) 
                ON [Scirra].[dbo].[tblForumGroup].group_id = [Scirra].[dbo].[tblForumAuthor].group_id) 
                LEFT JOIN [Scirra].[dbo].[tblForumGuestName] 
                ON [Scirra].[dbo].[tblForumThread].thread_id = [Scirra].[dbo].[tblForumGuestName].thread_id 
        WHERE


          [Scirra].[dbo].[tblForumThread].topic_id = 33854 
               AND ( [Scirra].[dbo].[tblForumThread].hide = 0 
                      OR [Scirra].[dbo].[tblForumThread].author_id = 13405 ))



                       AS 
       pagingquery 
WHERE  rownum BETWEEN 1 AND 10; 

我已经选择了字段:

 ,
 [Scirra].[dbo].[tblProfile].EmailAddress, 
 [Scirra].[dbo].[tblProfile].EmailVerified

但我有点坚持在那个巢中加入!

任何帮助表示赞赏!

编辑

对不起!我需要加入 tblProfile.UserID 和 tblAuthor.author_ID

【问题讨论】:

  • 你的tblProfile表的其他字段是什么?所以我们可以知道如何将它与其他表连接起来。
  • @Lamak 对不起!我需要将 tblProfile.UserID 与 tblAuthor.author_ID 加入
  • @Abe 是的,天哪,我累了,抱歉

标签: sql sql-server inner-join


【解决方案1】:

好的,如果我正确理解了您的查询,您可以试试这个:

SELECT * 
FROM   (SELECT TOP 10 [Scirra].[dbo].[tblForumThread].thread_id, 
                      [Scirra].[dbo].[tblForumThread].MESSAGE, 
                      [Scirra].[dbo].[tblForumThread].message_date, 
                      [Scirra].[dbo].[tblForumThread].show_signature, 
                      [Scirra].[dbo].[tblForumThread].ip_addr, 
                      [Scirra].[dbo].[tblForumThread].hide, 
                      [Scirra].[dbo].[tblForumAuthor].author_id, 
                      [Scirra].[dbo].[tblForumAuthor].username, 
                      [Scirra].[dbo].[tblForumAuthor].homepage, 
                      [Scirra].[dbo].[tblForumAuthor].location, 
                      [Scirra].[dbo].[tblForumAuthor].no_of_posts, 
                      [Scirra].[dbo].[tblForumAuthor].join_date, 
                      [Scirra].[dbo].[tblForumAuthor].SIGNATURE, 
                      [Scirra].[dbo].[tblForumAuthor].active, 
                      [Scirra].[dbo].[tblForumAuthor].avatar, 
                      [Scirra].[dbo].[tblForumAuthor].avatar_title, 
                      [Scirra].[dbo].[tblForumGroup].name 
                             AS groupname, 
                      [Scirra].[dbo].[tblForumGroup].stars, 
                      [Scirra].[dbo].[tblForumGroup].custom_stars, 
                      [Scirra].[dbo].[tblForumGuestName].name 
                             AS guestname, 
                      [Scirra].[dbo].[tblProfile].EmailAddress, 
                      [Scirra].[dbo].[tblProfile].EmailVerified,
                      Row_number() OVER (ORDER BY 
                             [Scirra].[dbo].[tblForumThread].message_date ASC) 
                      AS 
                      rownum
        FROM   [Scirra].[dbo].[tblForumGroup] 
                INNER JOIN [Scirra].[dbo].[tblForumAuthor] 
                ON [Scirra].[dbo].[tblForumGroup].group_id = [Scirra].[dbo].[tblForumAuthor].group_id
                INNER JOIN [Scirra].[dbo].[tblForumThread] 
                ON [Scirra].[dbo].[tblForumAuthor].author_id = [Scirra].[dbo].[tblForumThread].author_id
                LEFT JOIN [Scirra].[dbo].[tblForumGuestName] 
                ON [Scirra].[dbo].[tblForumThread].thread_id = [Scirra].[dbo].[tblForumGuestName].thread_id 
                LEFT JOIN [Scirra].[dbo].[tblProfile]
                ON [Scirra].[dbo].[tblProfile].UserID = [Scirra].[dbo].[tblForumAuthor].author_ID
        WHERE [Scirra].[dbo].[tblForumThread].topic_id = 33854 
              AND ([Scirra].[dbo].[tblForumThread].hide = 0 OR [Scirra].[dbo].[tblForumThread].author_id = 13405 )) AS pagingquery 
WHERE  rownum BETWEEN 1 AND 10; 

【讨论】:

    【解决方案2】:

    您需要在 tblProfile 上添加JOIN

    INNER JOIN [Scirra].[dbo].[tblProfile]   as tp
    ON tblAuthor.author_ID  = tp.user_id
    

    没有这个就没有办法参考

     [Scirra].[dbo].[tblProfile].EmailAddress, 
     [Scirra].[dbo].[tblProfile].EmailVerified
    

    你可以把它放在你的 tblForumAuthor JOIN 之后的任何地方

    【讨论】:

    • Abe 谢谢,但我不知道该把连接放在哪里,我对它们不太擅长,而且我一生都无法弄清楚如何把它和其他人放在一起
    • 您是想说 tblAuthor 还是上面说的 tblForumAuthor?我问是因为该表似乎不是您查询的一部分
    • 是的,对不起,今晚累了
    • 表名正确,我得到The multi-part identifier "Scirra.dbo.tblProfile.EmailAddress" could not be bound.
    • 谢谢,其他答案得到了它,但 +1 为您的帮助,再次感谢
    猜你喜欢
    • 2017-10-19
    • 2016-03-29
    • 2015-06-21
    • 2013-07-06
    • 2011-04-15
    • 2018-03-23
    • 2013-04-26
    • 2012-12-28
    • 1970-01-01
    相关资源
    最近更新 更多