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
相关文章:
1. android - 如何缩小APK的体积2. javascript - 奇怪的Symbol的问题3. position:absolute、float、display:inline-block 都能实现相同效果,区别是什么?4. css - 移动端 盒子内加overflow-y:scroll后 字体会变大5. angular.js - protractor初学 参考案例运行测试文件 报Error: Timeout6. javascript - 在vue-cli引入vux后 使用报错7. angular.js - angular post的Content-Type被设置,导致不能上传图片,求助!!8. javascript - JS new Date() 保存到 mongodb 中会早8个小时,我们这里是东八区,mongodb 保存的是格林尼治时间9. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””10. html - iframe嵌套网页在iPhone端的显示问题

网公网安备