求助mysql多表查询问题

hbgzg3006 2012-05-25 05:43:35
我有一张表A 主键id(自增)
令外有表B、C、D 他们的第一列都是A的主键ID的值(A的主键ID只可能在B、C、D的一个中出现)

我现在想实现这样的功能找出A的前10条记录和匹配到的表明。

非常感谢。
...全文
114 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
百年树人 2012-05-25
  • 打赏
  • 举报
回复
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;
  • 打赏
  • 举报
回复

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
hbgzg3006 2012-05-25
  • 打赏
  • 举报
回复
3分?大家有啥想法用sqlserver实现也行,我参考一下。
hbgzg3006 2012-05-25
  • 打赏
  • 举报
回复
我晕 怎么发到sqlserver了。。。
  • 打赏
  • 举报
回复
Mysql建议你去相应版块吧

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧