[英]SQL query 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
您需要在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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.