qq_37504599
2017-09-05 02:43
采纳率: 0%
浏览 4.5k

根据某表的字段的值不同如何关联查询不同表

表 a a.id,a.count
表b b.id ,b.to_id,b.from_id,b.rel 其中 a.id=to_id
表u u.userid, u.name
表c c.id,c.userid
表d d.id, d.userid
表 f f.id,f.userid
其中 if (b.rel =“c ”)b.from_id=c.id
else if (b.rel = “d” ) b.from_id=d.id
else if (b,rel = "f ” ) b.from_id = f.id

要通过 a的id查询出 a.count, userid, name;
Oracle 查询语句该如何写
菜鸟求救

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • 花下泥 2017-09-05 03:00

    select a.count,u.userid,u.name
    from a
    inner join b on a.id=b.to_id
    inner join c on b.from_id=c.id
    inner join u on c.userid=u.userid
    where b.rel='c'

    union

    select a.count,u.userid,u.name
    from a
    inner join b on a.id=b.to_id
    inner join d on b.from_id=d.id
    inner join u on d.userid=u.userid
    where b.rel='d'
    union

    select a.count,u.userid,u.name
    from a
    inner join b on a.id=b.to_id
    inner join f on b.from_id=f.id
    inner join u on f.userid=u.userid
    where b.rel='f'

    打赏 评论
  • mingbianshifei 2017-09-05 03:00

    select a.count, u.userid, u.name
    from a a
    left join b b on a.id = b.to_id
    left join c on b.from_id = c.id
    left join u on c.userid = u.userid
    where b.rel = 'c'
    union
    select a.count, u.userid, u.name
    from a a
    left join b b on a.id = b.to_id
    left join d on b.from_id = d.id
    left join u on d.userid = u.userid
    where b.rel = 'd'
    union
    select a.count, u.userid, u.name
    from a a
    left join b b on a.id = b.to_id
    left join f on b.from_id = f.id
    left join u on f.userid = u.userid
    where b.rel = 'f'

    这样应该可以,不过效率可能不高

    打赏 评论
  • qq_37504599 2017-09-05 03:57

    这样子a.id的判断条件放何处效率最高

    打赏 评论
  • cloudyzhao 2017-09-05 05:13

    为了效率 可以直接写存储过程嘛

    打赏 评论
  • 黄河默默 2017-09-05 06:03

    可以用sql 注入 参数注入 做查询

    打赏 评论

相关推荐 更多相似问题