--表一:
create table b
(
bid int primary key,
name varchar(20)
);
insert into b values(1,'张三');
insert into b values(2,'王五');
insert into b values(3,'小小小');
insert into b values(4,'但大大');
--表二:
create table a
(
aid int primary key,
zdrid int,
shrid int
);
insert into a values(1,1,3);
insert into a values(2,1,2);
insert into a values(3,1,1);
--查询aid=1的制单人(zdrid)和审核人(shrid)的名字
内连接更合理
[code="sql"]select zd.name,sh.name from a a
inner join b zd on a.zdrid = zd.bid
inner join b sh on a.shrid = sh.bid
where a.aid = 1[/code]
LZ试试这个看下
[code="sql"]
select a.aid,zd.name,sh.name from temp_a a ,
(select b.name from temp_a a, temp_b b where a.zdrid=b.bid and a.aid='1') zd,
(select b.name from temp_a a, temp_b b where a.shrid=b.bid and a.aid='1') sh
where a.aid='1';
[/code]
[code="sql"]select zd.name,sh.name from a a
left join b zd on a.zdrid = zd.bid
left join b sh on a.shrid = sh.bid
where a.aid = 1[/code]