日期:2014-05-16 浏览次数:20463 次
/****** Object: Table [dbo].[Table1] Script Date: 03/03/2014 15:51:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table1](
[id] [int] NOT NULL,
[useridA] [int] NULL,
[useridB] [int] NULL,
[Remark] [nvarchar](50) NULL,
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
[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
GO
/****** Object: Table [dbo].[Table2] Script Date: 03/03/2014 15:51:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table2](
[id] [int] NOT NULL,
[userName] [nvarchar](50) NULL,
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
(
[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
--插入数据
INSERT [Table1] ( [id] , [useridA] , [useridB] , [Remark] ) VALUES ( 1 , 1 , 2 , '3434' )
INSERT [Table1] ( [id] , [useridA] , [useridB] , [Remark] ) VALUES ( 2 , 2 , 3 , 'rewr' )
INSERT [Table1] ( [id] , [useridA] , [useridB] , [Remark] ) VALUES ( 3 , 1 , 1 , 'efsfsfds' )
INSERT [Table1] ( [id] , [useridA] , [useridB] , [Remark] ) VALUES ( 4 , 2 , 1 , '5454' )
INSERT [Table2] ( [id] , [userName] ) VALUES ( 1 , '张一' )
INSERT [Table2] ( [id] , [userName] ) VALUES ( 2 , '张二' )
INSERT [Table2] ( [id] , [userName] ) VALUES ( 3 , '张三' )
INSERT [Table2] ( [id] , [userName] ) VALUES ( 4 , '张四' )
SELECT a.id,b.userName,c.userName,a.Remark FROM table1 AS a INNER JOIN table2 AS b ON a.useridA=b.id INNER JOIN table2 AS c ON a.useridB=c.id
SELECT
a.id, b.userName, c.userName, a.Remark
FROM
table1 AS a
INNER JOIN table2 AS b
ON a.useridA=b.id
INNER JOIN table2 AS c
ON a.useridB=c