求一个复杂的查询

sdyqingdao 2009-09-29 03:31:41
有4个表,ISSUES(用户提交的issue基本信息) , MESSAGES(issue相关的消息) , DOCUMENTS(issue相关的附件), COMMENTS(用户和客服之间的电子邮件交流记录)。

每个issue有0~n个messages, 0~n个documents,0~n个comments

每个表中都有issue_ID字段,在ISSUE_DB_ISSUE中是主键,在其它三个表中是外键。

我要:在一个页面内显示所有的ISSUE,如果该ISSUE有documents, comments或者message也都显示出来。不考虑效率。

非常感谢!100分献上。
...全文
175 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhubidong 2009-09-30
  • 打赏
  • 举报
回复
2楼正解
数据娃掘 2009-09-29
  • 打赏
  • 举报
回复
codeselect * from
issues t1,
messages t2,
documents t3,
comments t4
where t1.issue_id= t2.issue_id(+)
and t1.issue_id= t3.issue_id(+)
and t1.issue_id= t4.issue_id(+)
orderby t1.issue_id;
laomeng520 2009-09-29
  • 打赏
  • 举报
回复
Left Join 可以解决这个问题
inthirties 2009-09-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shiyiwan 的回复:]
SQL codeselect*from issues t1,
messages t2,
documents t3,
comments t4where t1.issue_id= t2.issue_id(+)and t1.issue_id= t3.issue_id(+)and t1.issue_id= t4.issue_id(+)orderby t1.issue_id;
[/Quote]

up

改成left join的方式

select t1.* from issues left join messages t2 on t1.issue_id = t2.issue_id left join on documents t3 on t1.issue_id = t3.issue_id left join on comments t4 on t1.issue_id = t4.issue_id

和(+)是一样的方式,不过是用标准的left join实现的。
璇之星 2009-09-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 amiksong 的回复:]
引用 1 楼 shiyiwan 的回复:
SQL codeselect*from  issues t1,
      messages t2,
      documents t3,
      comments t4where t1.issue_id= t2.issue_id(+)and  t1.issue_id= t3.issue_id(+)and  t1.issue_id= t4.issue_id(+)orderby t1.is¡­


正解
[/Quote]up!
cosio 2009-09-29
  • 打赏
  • 举报
回复
传说中的非常难,过来看看了!
hebo2005 2009-09-29
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yulan_119 的回复:]
引用 10 楼 inthirties 的回复:
引用 1 楼 shiyiwan 的回复:
SQL codeselect*from  issues t1,
      messages t2,
      documents t3,
      comments t4where t1.issue_id= t2.issue_id(+)and  t1.issue_id= t3.issue_id(+)and  t1.issue_id= t4.issue_id(+)orderby t1.issue_id;


up

改成left join的方式

select t1.* from issues left join messages t2 on t1.issue_id = t2.issue_id left join on documents t3 on t1.issue_id = t3.issue_id left join on comments t4 on t1.issue_id = t4.issue_id

和(+)是一样的方式,不过是用标准的left join实现的。




我弱得问一句,上边加(+)的那个是“右连接”吧?! 
[/Quote]

+在右边的是左连接,在左边的是右连接
你可以想像一下,在右边的,就是左边的数据比右边的数据多
amiksong 2009-09-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shiyiwan 的回复:]
SQL codeselect*from issues t1,
messages t2,
documents t3,
comments t4where t1.issue_id= t2.issue_id(+)and t1.issue_id= t3.issue_id(+)and t1.issue_id= t4.issue_id(+)orderby t1.is¡­
[/Quote]

正解
wxkang123 2009-09-29
  • 打赏
  • 举报
回复
2楼正解
iqlife 2009-09-29
  • 打赏
  • 举报
回复
UP2楼,接分
jdsnhan 2009-09-29
  • 打赏
  • 举报
回复
左连接
wen1512 2009-09-29
  • 打赏
  • 举报
回复
left join应该就能解决问题吧
小灰狼W 2009-09-29
  • 打赏
  • 举报
回复
一个外连接就解决了
不知是不是没理解楼主的问题
代码参照楼上,我就不写了
shiyiwan 2009-09-29
  • 打赏
  • 举报
回复
select * 
from issues t1,
messages t2,
documents t3,
comments t4
where t1.issue_id = t2.issue_id(+)
and t1.issue_id = t3.issue_id(+)
and t1.issue_id = t4.issue_id(+)
order by t1.issue_id;
yulan_119 2009-09-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 inthirties 的回复:]
引用 1 楼 shiyiwan 的回复:
SQL codeselect*from  issues t1,
      messages t2,
      documents t3,
      comments t4where t1.issue_id= t2.issue_id(+)and  t1.issue_id= t3.issue_id(+)and  t1.issue_id= t4.issue_id(+)orderby t1.issue_id;


up

改成left join的方式

select t1.* from issues left join messages t2 on t1.issue_id = t2.issue_id left join on documents t3 on t1.issue_id = t3.issue_id left join on comments t4 on t1.issue_id = t4.issue_id

和(+)是一样的方式,不过是用标准的left join实现的。

[/Quote]


我弱得问一句,上边加(+)的那个是“右连接”吧?!
oraclemch 2009-09-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 notebook800 的回复:]
明明1楼的正解,为啥都说是2楼的。
[/Quote]

因为2楼的名气大啊,以大压小啊!^_^!

只怪2楼最近人气太高了!(*^__^*) 嘻嘻……!
notebook800 2009-09-29
  • 打赏
  • 举报
回复
明明1楼的正解,为啥都说是2楼的。

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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