高难度,多表查询

yxandyx 2005-05-19 06:11:27
四个表相关字段:
===============
t1.user_id

t2.user_id,msg_id

t3.msg_id,vid,jid,eid

t4.money,vid,jid,eid

问题来了,要依靠 user_id 取出相应的 money.注:各表中相同字段有关联(即有相等的记录).
谢谢.
...全文
368 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
xw916 2005-05-24
  • 打赏
  • 举报
回复
up
sullivanzuo 2005-05-23
  • 打赏
  • 举报
回复
select t4.money,t1.user_id from t1,t2,t3,t4
where t1.user_id = '變量'
and t1.user_id = t2.user_id
and t2.msg_id = t3.msg
and t3.vid = t4.vid
and t3.jid = t4.jid
and t3.eid = t4.eid
zzwind5 2005-05-23
  • 打赏
  • 举报
回复
select a.money from (select money, t4.vid, t4.jid, t4.eid, msg_id from t3, t4
where t4.vid=t3.vid and
t4.jid=t3.jid and
t4.eid=t3.eid) a
where a.msg_id in (select msg_id from t2, t1 where t2.user_id=t1.user_id )
guangli_zhang 2005-05-23
  • 打赏
  • 举报
回复
select t.money from t4,t3,t2,t1
where t1.user_id = t2.user_id and t2.msg_id = t3.msg_id and t3.vid = t4.vid and t3.jid = t4.jid and t3.eid = t4.eid
tangtangno1 2005-05-23
  • 打赏
  • 举报
回复
heyixiang(子豚の愛人)
不建议用in

这说明你还不真正理解in是怎么回事,不要被一般人误解的那样把in当作“臭名拙著的goto”一样,所有的语言编译的最后结果,无非还是机器语言或者汇编语言中的goto(条件转移jump)。对于这些实现相同功能的不同语法词汇的编译执行策略,需要真正了解的东西还很多,否则数据库标准和作者们也就不会费劲做出这种多余的语法来了
tangtangno1 2005-05-23
  • 打赏
  • 举报
回复
对于此类特定情况的多表查询,根据oracle对sql语法编译解释的一些规律,可以写出同样功能但效率差异很大的不同sql来。
一般多表连接,肯定还有多个列之间的条件,这样oracle进行语法分析、寻找各个表列的索引排列组合进行优化查找分析需要花费较大的资源,简单的说也就是数据结构上经常提到的成指数增长的时间复杂度O(m,n)(mn分别代表了表的数量和索引列的数量)
如果这众多表查询正向上边楼主遇到的情况:根据一个表里查询出来的结果,当作条件去查询另外一个表,以此类推的多表查询,而每次查询都能用到索引或者主键列,每一个子查询的结果集又数量有限,那么采用...in(...in(...))这样的形式效率会比较高。因为这种情况下最内层的子查询用到了主健或者索引条件,可以认为在瞬间就能查到结果集,这个结果集又只是数量有限的几条记录,这几条记录又是它外层查询的主键或者索引条件,这样又能瞬间查到外层结果集,以次类推,这样效率就高了。而且因为每一层的in()查询都是简单的单表查询,oracle耗费的语法分析索引分析等资源就非常少,差不多可以认为是时间复杂度O(1),常数时间就可以解决
对于效率和使用索引、索引优化等问题,都可以回顾以前学过的数据结构,数据库无非就是一个各种查找排序的实现而已
tangtangno1 2005-05-20
  • 打赏
  • 举报
回复
select money from t4 where (vid,jid,eid) in(select vid,jid,eid from t3 where msg_id in(select msg_id from t2 where user_id in(select user_id from t1 where user_id='...')))
或者
select money from t4 where (vid,jid,eid) in(select vid,jid,eid from t3 where msg_id in(select msg_id from t2 where user_id='...'))
这样显得比较清晰明了
wxvp 2005-05-20
  • 打赏
  • 举报
回复


select t4.money form t4 where t1.user_id=t2.user_id and t2.msg_id=t3.msg_id and t3.vid=t4.vid and t1.user_id="...."
yxandyx 2005-05-20
  • 打赏
  • 举报
回复
还有其他方式吗,通过联接实现
yxandyx 2005-05-20
  • 打赏
  • 举报
回复
tangtangno1(糖糖):不错.
hcom 2005-05-20
  • 打赏
  • 举报
回复
up 楼上的sql执行效率怎样?因为我也遇到同样的问题,不知道怎样解决。
谢谢
yxandyx 2005-05-20
  • 打赏
  • 举报
回复
ojaoja():不能通过.
heyixiang 2005-05-20
  • 打赏
  • 举报
回复
不建议用in
ojaoja 2005-05-20
  • 打赏
  • 举报
回复
给你个正解吧:
select t4.money,a.user_id
from t4,
(select t1.userid,t3.vid,t3.jid,t3.eid
from t1,t2,t3
where t1.user_id=t2.user_id
and t2.msg_id=t3.msg_id
) a
where (a.vid,a.jid,a.eid) in (t4.vid,t4.jid,t4.eid)
yxandyx 2005-05-20
  • 打赏
  • 举报
回复
wxvp(wxvp):t3和t4需要三个条件匹配
yxandyx 2005-05-19
  • 打赏
  • 举报
回复
不能用一个查询解决吗?
xjlsj 2005-05-19
  • 打赏
  • 举报
回复
t1 和 t2 建一个视图 v1 ,t3和t4建一个视图v2 v1和v2做连接
yxandyx 2005-05-19
  • 打赏
  • 举报
回复
这就不好查询了是不是?
yxandyx 2005-05-19
  • 打赏
  • 举报
回复
xwdboy:
t3和t4之间需要三个字段同时匹配才行即:t3.vid=t4.vid,t3.jid=t4.jid,t3.eid=t4.eid.
xwqboy 2005-05-19
  • 打赏
  • 举报
回复
select t4.money from t1,t2,t3,t4 where t1.user_id=t2.user_id and t2.msg_id=t3.msg_id and t3.vid=t4.vid and t1.user_id='数值'

17,378

社区成员

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

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