sql一张表和另一张表多次内联

--创建班级表
create table ClassTable
(
	classID int primary key identity(1,1),
	teacherID int,
	studentID int
)
--创建用户表
create table UserTable
(
	userID int primary key identity(1,1),
	userName nvarchar(50)
)

--往班级表插入数据
insert into ClassTable(teacherID,studentID) values(1,2);
--往用户表插入数据
insert into UserTable(userName)values('张老师');
insert into UserTable(userName)values('张学生');

--一张表和另一张表多次内联
select 
ClassTable.classID,
a.userName as teacherName, 
b.userName as studentName 
from ClassTable
inner join UserTable as a
on ClassTable.teacherID=a.userID
inner join UserTable as b
on ClassTable.studentID=b.userID

你可能感兴趣的:(sql,server,sql,内联)