联接临时表以在SQL中查询结果

联接临时表以在SQL中查询结果,sql,sql-server,Sql,Sql Server,我对SQL相当陌生,正在寻找一种简单/简洁的方法来实现以下目标: 我有一个查询,它有一个嵌套的select语句,这个嵌套的select将数据返回到一个临时表。我希望这个临时表能够加入到初始返回中,但是,我不确定如何实现这一点 SELECT Profile.profilename as Recipient, Message.messagetext as MessageText, message.datesent as DateSent FROM Profile INNER JOIN Mes

我对SQL相当陌生,正在寻找一种简单/简洁的方法来实现以下目标:

我有一个查询,它有一个嵌套的select语句,这个嵌套的select将数据返回到一个临时表。我希望这个临时表能够加入到初始返回中,但是,我不确定如何实现这一点

SELECT Profile.profilename as Recipient, 
Message.messagetext as MessageText, 
message.datesent as DateSent

FROM Profile 
INNER JOIN
Message ON Profile.profile_id = Message.profile_idtoo
WHERE Message.profile_idfrom = 3

(
SELECT profilename  from Profile
    Inner JOIN 
    Message on Profile.profile_id = Message.profile_idfrom
    WHERE Message.profile_idfrom = 3
) 
--创建表和数据

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Message](
    [message_id] [int] IDENTITY(1,1) NOT NULL,
    [messagetext] [nvarchar](max) NOT NULL,
    [datesent] [date] NOT NULL,
    [profile_idtoo] [int] NOT NULL,
    [profile_idfrom] [int] NOT NULL,
 CONSTRAINT [Message_pk] PRIMARY KEY CLUSTERED 
(
    [message_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Profile]    Script Date: 20/11/2018 13:14:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Profile](
    [profile_id] [int] IDENTITY(1,1) NOT NULL,
    [profilename] [nvarchar](255) NOT NULL,
    [regdate] [date] NOT NULL,
    [user_id] [int] NOT NULL,
 CONSTRAINT [Profile_pk] PRIMARY KEY CLUSTERED 
(
    [profile_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Message] ON 

INSERT [dbo].[Message] ([message_id], [messagetext], [datesent], [profile_idtoo], [profile_idfrom]) VALUES (1, N'This playlist is the best', CAST(N'2018-11-13' AS Date), 9, 8)
INSERT [dbo].[Message] ([message_id], [messagetext], [datesent], [profile_idtoo], [profile_idfrom]) VALUES (2, N'I got superscared by a few of those moviews tbh', CAST(N'2017-12-14' AS Date), 2, 3)
INSERT [dbo].[Message] ([message_id], [messagetext], [datesent], [profile_idtoo], [profile_idfrom]) VALUES (3, N'I think you should look atmy playlist called "the best songs of all time". ', CAST(N'2017-09-14' AS Date), 2, 5)
INSERT [dbo].[Message] ([message_id], [messagetext], [datesent], [profile_idtoo], [profile_idfrom]) VALUES (4, N'Best thing I ever done signin up to this site!', CAST(N'2018-11-13' AS Date), 6, 7)
SET IDENTITY_INSERT [dbo].[Message] OFF
SET IDENTITY_INSERT [dbo].[Profile] ON 

INSERT [dbo].[Profile] ([profile_id], [profilename], [regdate], [user_id]) VALUES (1, N'TheSpazzCommander', CAST(N'2017-02-01' AS Date), 1)
INSERT [dbo].[Profile] ([profile_id], [profilename], [regdate], [user_id]) VALUES (2, N'CaptainBuzzkill', CAST(N'2015-01-01' AS Date), 1)
INSERT [dbo].[Profile] ([profile_id], [profilename], [regdate], [user_id]) VALUES (3, N'EVLM', CAST(N'2018-08-15' AS Date), 2)
INSERT [dbo].[Profile] ([profile_id], [profilename], [regdate], [user_id]) VALUES (4, N'JBlunt', CAST(N'2017-07-15' AS Date), 3)
INSERT [dbo].[Profile] ([profile_id], [profilename], [regdate], [user_id]) VALUES (5, N'JaneHeart', CAST(N'2015-05-01' AS Date), 4)
INSERT [dbo].[Profile] ([profile_id], [profilename], [regdate], [user_id]) VALUES (6, N'JimmyHeart', CAST(N'2015-01-05' AS Date), 4)
INSERT [dbo].[Profile] ([profile_id], [profilename], [regdate], [user_id]) VALUES (7, N'Khall', CAST(N'2014-01-01' AS Date), 6)
INSERT [dbo].[Profile] ([profile_id], [profilename], [regdate], [user_id]) VALUES (8, N'thehunter', CAST(N'2017-01-01' AS Date), 7)
INSERT [dbo].[Profile] ([profile_id], [profilename], [regdate], [user_id]) VALUES (9, N'thehunterswife', CAST(N'2017-01-01' AS Date), 7)
SET IDENTITY_INSERT [dbo].[Profile] OFF
ALTER TABLE [dbo].[Message]  WITH CHECK ADD  CONSTRAINT [message_Profile_from] FOREIGN KEY([profile_idfrom])
REFERENCES [dbo].[Profile] ([profile_id])
GO
ALTER TABLE [dbo].[Message] CHECK CONSTRAINT [message_Profile_from]
GO
ALTER TABLE [dbo].[Message]  WITH CHECK ADD  CONSTRAINT [message_Profile_too] FOREIGN KEY([profile_idtoo])
REFERENCES [dbo].[Profile] ([profile_id])
GO
ALTER TABLE [dbo].[Message] CHECK CONSTRAINT [message_Profile_too]
GO
ALTER TABLE [dbo].[Profile]  WITH CHECK ADD  CONSTRAINT [Profile_User] FOREIGN KEY([user_id])
REFERENCES [dbo].[User] ([user_id])
GO
ALTER TABLE [dbo].[Profile] CHECK CONSTRAINT [Profile_User]
GO
谢谢你的帮助

你可以在下面试试

    SELECT Profile.profile_id,Profile.profilename as Recipient, m1.profilename,
Message.messagetext as MessageText, 
message.datesent as DateSent
FROM Profile 
INNER JOIN
Message ON Profile.profile_id = Message.profile_idtoo
inner join (SELECT profile_id,profilename  from Profile
    Inner JOIN 
    Message on Profile.profile_id = Message.profile_idfrom
    WHERE Message.profile_idfrom = 3) m1 on Message.profile_idfrom = m1.profile_id
WHERE Message.profile_idfrom = 3

在不知道您还需要从结果集中的第二个表中获取什么的情况下,这可能也会起作用……请记住,您在两个不同的字段上有来自同一个表的内部联接,您可能正在排除结果,也可能是预期的结果:)更多信息将非常有用

SELECT  PROFILE.profilename AS Recipient, 
        MessageToo.messagetext AS MessageText, 
        MessageToo.datesent AS DateSent
FROM PROFILE
INNER JOIN Message MessageToo ON PROFILE.profile_id = Message.profile_idtoo
INNER JOIN Message MessageFrom ON PROFILE.profile_id = Message.profile_idfrom
WHERE Message.profile_idfrom = 3


请提供示例数据、所需结果以及不描述非功能查询的逻辑说明。我将挖掘示例数据,基本上是查询函数,并返回我想要的结果,我只想将第二个select连接到初始select,这样在运行查询时不会返回两个表。您的代码不包含、引用或创建任何临时表或表变量,因此不清楚您的目标是什么。“Join”也有一个特定的含义,但我得到的印象是,您只需将2个结果集合并为1个。生成第一个查询的行,然后将第二个查询中的其他行添加到该集合中。如果是这样,union操作符可能会很有用。但我认为你可能在努力变得聪明或高效(或更糟)。我的讲师告诉我,第二个选择被归类为临时表。你确实是对的,我只是想把返回的结果合并起来。我尝试了一下,它没有返回任何数据给我。这是我的第一篇SQL帖子,所以我想我需要提供更多的内容。@Simondan是的,这将非常有帮助。表和插入脚本是否足以让人使用?对不起,我的绿色色调。@SimonDean-我已经编辑了我的答案-您现在也可以检查-@SimonDean,是的,您可以这样做。我故意尝试排除结果,因为目的是显示从用户3发送到目标用户的所有消息。如果这是您需要的,您可能不需要第二个表联接?我在上面添加了额外的逻辑。我发布的初始select查询返回以下内容:收件人、消息和发送日期。但是,我也尝试返回发送消息的用户的名称(该名称存在于profile表中)。返回发件人名称时要引用的FK是Message.profile_idfrom。
SELECT  PROFILE.profilename AS Recipient, 
        MessageToo.messagetext AS MessageText, 
        MessageToo.datesent AS DateSent
FROM PROFILE
LEFT JOIN Message MessageToo ON PROFILE.profile_id = Message.profile_idtoo
WHERE PROFILE.profile_idfrom = 3