22,210
社区成员
发帖
与我相关
我的任务
分享
--创建BILL测试表
if object_id('bill') is not null drop table bill
create table bill (id int,[name] varchar(20))
insert into bill
select 1,'aaa' union all
select 2,'bbb'
--创建bills测试表
if object_id('bills') is not null drop table bills
create table bills (bid int,bname varchar(20),pid int )
insert into bills
select 1,'ccc',3 union all
select 2,'ddd',4
--查询
select a.id,b.[bname],a.[name],c.pid from bill a
join bills b on a.id=b.pid
select id,name
,bname=(select top 1 name from bills where pid =id )
from bill
select m.* , n.name from bill m, bills n where m.id = n.pid
select m.* , (select name from bills n where n.pid = m.id) name from bill m
select a.id,a.name
,(select name from bills where pid =a.id )
from bill a