SQL查询内连接

时间:2011-04-11 21:04:57

标签: sql sql-server inner-join

我想修改论坛代码中的查询,从表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

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

任何帮助表示赞赏!

修改

抱歉!我需要使用tblAuthor.author_ID

加入tblProfile.UserID

2 个答案:

答案 0 :(得分:1)

您需要在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

之后的任何位置

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