需求:
查询F,并获取到E,D,C,B,A某个字段。
关系依次是:A>B>C>D>E>F
原本以为是so easy的需求,一会儿就可以喝茶了,实际没有这么简单。
试了SQL的子循环,左右连接,union都不可,最后sql查询结构为:
select
a.name,a.code,b.name,b.fid,c.name,d.name,e.name,f.name
from
(select a.name,a.code from atable a,common c where a.id = c.id ) a,
(select b.name,b.fid from btable b,common b where b.id = c.id ) b,
(select c.name from ctable c,common c where c.id = c.id ) c,
(select d.name from dtable d,common d where d.id = c.id ) d,
(select e.name from etable a,common e where e.id = c.id ) e
where
a.name like '%%';
a和b中只是列举了不同的字段,在开发中可以把需要的字段都查出,并在最外层的select中查询出即可。
另外:一定要注意表与表直接的连接,可能会影响到查询效率。