日期:2014-05-17 浏览次数:20655 次
create table ACCOUNT(ACCOUNTID varchar(10), NAME varchar(10), TYPE varchar(10))
insert into ACCOUNT
select 'A001', 'JOHN', 'TUTOR' union all
select 'A002', 'MIKE', 'TUTOR' union all
select 'A003', 'JIM', 'STUDENT' union all
select 'A004', 'LILY', 'STUDENT'
create table APPOINTMENT(STUDENTID varchar(10), TUTORID varchar(10))
insert into APPOINTMENT
select 'A003', 'A001' union all
select 'A004', 'A002'
go
select (select NAME from ACCOUNT a where a.ACCOUNTID = aa.STUDENTID) STUDENTNAME,
(select NAME from ACCOUNT a where a.ACCOUNTID = aa.TUTORID) TUTORNAME
from APPOINTMENT aa
/*
STUDENTNAME TUTORNAME
JIM JOHN
LILY MIKE
*/
create table ACCOUNT
(ACCOUNTID varchar(10),name varchar(10),TYPE varchar(10))
insert into ACCOUNT
select 'A001','JOHN','TUTOR' union all
select 'A002','MIKE','TUTOR' union all
select 'A003','JIM','STUDENT' union all
select 'A004','LILY','STUDENT'
create table APPOINTMENT
(STUDENTID varchar(10),TUTORID varchar(10))
insert into APPOINTMENT
select 'A003','A001' union all
select 'A004','A002'
select b.name 'STUDENTNAME',c.name 'TUTORNAME'
from APPOINTMENT a
left join ACCOUNT b on a.STUDENTID=b.ACCOUNTID
left join ACCOUNT c on a.TUTORID=c.ACCOUNTID
/*
STUDENTNAME TUTORNAME
----------- ----------
JIM JOHN
LILY MIKE
(2 row(s) affected)
*/