日期:2014-05-18 浏览次数:20436 次
declare @str varchar(10) set @str='' if exists (select 1 from B where id in(select top 10 id from A)) begin set @str='tbl_A' if exists (select 1 from C where id in(select top 10 id from A)) BEGIN set @str=@str+'tbl_B' if exists (select 1 from D where id in(select top 10 id from A)) begin set @str=@str+'tbl_D' end END print '匹配到的表为:'+@str end --try
------解决方案--------------------
select a.*,if(b.aid is not null,'B',if(c.aid is not null,'C','D')) as tableName from a left join b on a.id=b.aid left join c on a.id=c.aid left join d on a.id=d.aid order by a.id limit 10;