mysql - oracle 多表查询问题-笛卡儿积过滤
问题描述
问题如下:
查询出现笛卡儿积过滤的问题 例子: A B C D E Q表
select e.name, e.age, e.phone, q.something from a a, b b, c c, d d, e e, q q where a.id = ’zhejiushiyige id’ and b.id = a.bid and c.id = b.cid and d.id = c.did and e.id = d.eid and q.aid = a.id
就是表e是从a开始一级一级比较下来的,最终得到的e的结果是正确的,但是q的结果会出现两次,并且q只跟a有关联,请问怎么查询才能解决这个问题呢?
问题解答
回答1:q表和a表是一对多的关系,如果q表的结果只想出一条,可以在关联前先把q表按照aid字段进行汇总,保证每个aid只有一条,如:
select e.name, e.age, e.phone, q.something from a a, b b, c c, d d, e e, (select aid, max(something)) from q group by aid) table_q where a.id = ’zhejiushiyige id’ and b.id = a.bid and c.id = b.cid and d.id = c.did and e.id = d.eid and table_q.aid = a.id
相关文章:
