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. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。2. javascript - 如何將一個div始終固定在某個位置;無論屏幕和分辨率怎么變化;div位置始終不變3. html5 - 有可以一次性把所有 css外部樣式轉為html標簽內style=" "的方法嗎?4. javascript - 有什么比較好的網頁版shell前端組件?5. java - 如何寫一個intellij-idea插件,實現編譯時修改源代碼的目的6. javascript - 原生canvas中如何獲取到觸摸事件的canvas內坐標?7. java 中Long 類型如何轉換成Double?8. javascript - 求解答:實例對象調用constructor,此時constructor內的this的指向?9. html - vue項目中用到了elementUI問題10. javascript - [js]為什么畫布里不出現圖片呢?在線等
