如何对几个具有相同字段名的表进行搜索?

mysummer2003 2003-09-15 05:58:55
如何对几个具有相同字段名的表进行搜索
如a b c 都有id号,id号在几个表中都唯一
现在要在它们中间找出id=33的记录,表结构不同。
...全文
70 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zarge 2003-09-16
  • 打赏
  • 举报
回复
if exists(select 1 from a where id = 33)
select * from a where id = 33
else if exists(select 1 from b where id = 33)
select * from b where id = 33
else if exists(select 1 from c where id = 33)
select * from c where id = 33
yonghengdizhen 2003-09-15
  • 打赏
  • 举报
回复
输出列如果相同(数据类型兼容)的话,那就用
select column1,...columnn from tablename where id=xxxx
union
select col1,......coln from tablename2 where id=xxxx
.......

如果某表列数不足那就按,数据类型进行一一对应并输出null补充不足列作为输出
TaoGeGe 2003-09-15
  • 打赏
  • 举报
回复
select a.*,b.*,c.* from a,b,c wher a.id=33 or b.id=33 or c.id=33
這樣就可以查出id=33的記錄的字段了
1ssp 2003-09-15
  • 打赏
  • 举报
回复
if exists(select * from a where id=33)
select * from a
else
begin
if exists(select * from b where id=33)
select * from b
else
begin
if exists(select * from c where id=33)
select * from c
end
end
你试试吧!
mysummer2003 2003-09-15
  • 打赏
  • 举报
回复
不是要a,b,c所有的字段,而是33在哪个表中,就去哪个表的字段。
arrow_gx 2003-09-15
  • 打赏
  • 举报
回复
select a.*,b.*,c.* from a,b,c wher a.id=33 or b.id=33 or c.id=33

34,874

社区成员

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

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