2 qq 37504599 qq_37504599 于 2017.09.05 10:43 提问

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

表 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个回答

qq_26433213
qq_26433213   2017.09.05 11: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
mingbianshifei   2017.09.05 11: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
qq_37504599   2017.09.05 11:57

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

zy841958835
zy841958835   Ds   Rxr 2017.09.05 13:13

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

sj18182656342
sj18182656342   2017.09.05 14:03

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

Csdn user default icon
上传中...
上传图片
插入图片