SQL查询内部联接
我想修改我的论坛代码中的一个查询,从表tblProfile返回两个字段。此表有两个字段,“EmailAddress”和“EmailVerified” 这是原始查询:SQL查询内部联接,sql,sql-server,inner-join,Sql,Sql Server,Inner Join,我想修改我的论坛代码中的一个查询,从表tblProfile返回两个字段。此表有两个字段,“EmailAddress”和“EmailVerified” 这是原始查询: SELECT * FROM (SELECT TOP 10 [Scirra].[dbo].[tblForumThread].thread_id, [Scirra].[dbo].[tblForumThread].MESSAGE, [Scir
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与tblautor.author\u ID一起加入tblProfile.UserID。您需要在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
之后的任意位置。好的,如果我正确理解了您的查询,您可以尝试以下操作:
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;
您的
tblProfile
表的其他字段是什么?这样我们就可以知道如何将它与其他表连接起来。@Lamak抱歉!我需要加入tblProfile.UserID和tblAuthor.author_ID@Abe是的,天哪,我累了,对不起,谢谢,但我一直在想该把加入放在哪里,我不太擅长,我一辈子都不知道该怎么把它和其他人放在一起。你是说TBlautor还是说tblForumAuthor?我这样问是因为该表似乎不是您的查询的一部分。是的,对不起,今晚的表名是对的,我得到了无法绑定的多部分标识符“Scirra.dbo.tblProfile.EmailAddress”。
谢谢,其他答案得到了,但是+1感谢您的帮助,再次感谢