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

时间:2018-11-20 12:32:18

标签: sql sql-server

我对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

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您可以在下面尝试

DEMO

    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

答案 1 :(得分:1)

在不知道结果集中第二个表还想要什么的情况下,这也可能会起作用...请记住,您在两个不同字段上具有来自同一表的内部联接,您可能会排除结果,或者可能是预期结果:)更多信息会有所帮助!

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  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