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. 网页爬虫 - 关于Python的编码与解码问题2. css3 - 微信前端页面遇到的transition过渡动画的bug3. css - input间的间距和文字上下居中4. javascript - router.push无效5. javascript - postcss-loader在webpack2的使用.6. javascript - 为什么var obj = {}创建对象的方法里面不能用this.xxx来声明属性 ?7. css3 - 微信小程序如何把radio改成2个选择按钮的样式8. javascript - Storage中removeItem在什么情况下使用9. selenium-selenium-webdriver - python 将当前目录加入到 环境变量10. javascript - 浏览器回退,如何保证js对dom的操作保存下来

网公网安备