这个SQL如何写???

Delphi_Li 2003-01-23 08:55:53
有以下的表:
Table_A

ID Info_A
---------------
01 ABC
02 BCD
03 CDE

Table_B

ID Info_B
---------------
01 XYZ
03 QWE

现在想这样显示:

ID Info_A Info_B
--------------------------
01 ABC XYZ
02 BCD
03 CDE QWE

如何去做???
...全文
37 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyberflying 2003-01-23
  • 打赏
  • 举报
回复
SELECT a.id,a.info_a,b.b_info
FROM table_a a,table_b b
WHERE a.id = b.id
UNION ALL
SELECT a.id,a.info_a,b.b_info
FROM table_a a,table_b b
WHERE a.id = b.id(+)
AND b.id IS NULL
UNION ALL
SELECT a.id,a.info_a,b.b_info
FROM table_a a,table_b b
WHERE a.id(+) = b.id
AND a.id IS NULL
ORDER BY 1
liuxum 2003-01-23
  • 打赏
  • 举报
回复
只好用联合了--union
bobfang 2003-01-23
  • 打赏
  • 举报
回复
select a.id,a.info_a,b.b_info
from table_a a,table_b b
where a.id=b.id
union all
select a.id,a.info_a,' '
from table_a a
where not exists(select 1 from table_b b where a.id=b.id)
union all
select a.id,' ',b.info_b
from table_b b
where not exists(select 1 from table_a a where a.id=b.id)
order by 1
bobfang 2003-01-23
  • 打赏
  • 举报
回复
select a.id,a.info_a,b.b_info
from table_a a,table_b b
where a.id=b.id
union all
select a.id,a.info_a,' '
from table_a a
where not exists(select 1 from table_b where a.id=b.id)
union all
select a.id,' ',b.info_b
from table_b b
where not exists(select 1 from table_a where a.id=b.id)
order by 1
Lastdrop 2003-01-23
  • 打赏
  • 举报
回复
同样呀,只不过将table_a,和table_b分别换成子查询就行了。
Delphi_Li 2003-01-23
  • 打赏
  • 举报
回复
Table_A,Table_B中的数据时通过查询实现的!!!又该如何去做???
liuxum 2003-01-23
  • 打赏
  • 举报
回复
你所需要的数据主要是从哪个表取出的,那个表就是主表阿。你希望所后的结果是Info_A还是Info_B更重要些?哪个重要哪个就做主表。

Delphi_Li 2003-01-23
  • 打赏
  • 举报
回复
关键我不知道那个是主表,那个是附表!!!
liuxum 2003-01-23
  • 打赏
  • 举报
回复
那就这样啦:
select A.ID,A.Info_A,B.Info_B from Table_A A,Table_B B where B.ID=A.ID(+) order by A.iD
总之主表,附表的关系一般是这样的:主表.col=附表.col(+)
Delphi_Li 2003-01-23
  • 打赏
  • 举报
回复
To ghl200(小面人) 如果出现A表少,而B表多的情况,该如何处理呢???
谢谢!!!
ghl200 2003-01-23
  • 打赏
  • 举报
回复
select A.ID,A.Info_A,B.Info_B from Table_A A,Table_B B where A.ID=B.ID(+) order by A.iD
ghl200 2003-01-23
  • 打赏
  • 举报
回复
select A.ID,A.Info_A,B.Info_B from Table_A A,Table_B B where A.ID=B.ID(+)
yanleigis 2003-01-23
  • 打赏
  • 举报
回复
select a.id,a.info_a,b.b_info from table_a a,table_b b where a.id=b.id
order by a.id

17,090

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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