2015-02-24 80 views
0

我得到這個查詢:多個LEFT JOIN - 輸出錯誤

SELECT r.name, date(r.join_date) as join_date, 
(COUNT(DISTINCT a.id) + COUNT(DISTINCT n.id)) as num_likes, 
COALESCE(SUM(rv.views), 0) as sum_views 
FROM restaurants r 
LEFT JOIN apple_likes a ON r.id = a.restaurant_id 
LEFT JOIN android_likes n ON r.id = n.restaurant_id 
LEFT JOIN restaurant_views rv on r.id = rv.restaurant_id 
WHERE r.id=192 
GROUP BY r.id DESC 

enter image description here

這裏是與num_like變量和LEFT JOIN的另一個查詢中刪除:

SELECT r.name, date(r.join_date) as join_date, 
COALESCE(SUM(rv.views), 0) as sum_views 
FROM restaurants r 
LEFT JOIN restaurant_views rv on r.id = rv.restaurant_id 
WHERE r.id=192 
GROUP BY r.id DESC 

enter image description here

正如你所看到的,sum_views更改爲1793這是正確的!
這意味着,當我刪除其他LEFT JOIN的時候,sum_views顯示正確的值。
如何讓上方的查詢在sum_views上顯示正確的值?
(我可以,如果要求上傳其他表的結構的圖像!)


編輯:
我發現使用子查詢來解決這個問題

SELECT r.name, date(r.join_date) as join_date, 
(COUNT(DISTINCT a.id) + COUNT(DISTINCT n.id)) as num_likes, 
(SELECT COALESCE(SUM(views), 0) FROM restaurant_views WHERE restaurant_id=192) as sum_views 
FROM restaurants r 
LEFT JOIN apple_likes a ON r.id = a.restaurant_id 
LEFT JOIN android_likes n ON r.id = n.restaurant_id 
WHERE r.id=192 
GROUP BY r.id DESC 
方式

如果有人知道如何解決這個問題,而不使用子查詢,我會有興趣聽到它!

+0

如果您使用Gordon查詢中派生的表/內聯視圖'rv',我懷疑它會起作用。 – shawnt00 2015-02-24 01:25:46

回答

1

你的join s乘以行數。一種解決方案是預彙總數據的連接之前:

SELECT r.name, date(r.join_date) as join_date, 
     (coalesce(a.cnt, 0) + coalesce(n.cnt, 0)) as num_likes, 
     COALESCE(SUM(rv.views), 0) as sum_views 
FROM restaurants r LEFT JOIN 
    (select a.restaurant_id, count(*) as cnt 
     from apple_likes a 
     group by a.restaurant_id 
    ) a 
    ON r.id = a.restaurant_id LEFT JOIN 
    (select n.restuarant_id, count(*) as cnt 
     from android_likes n 
     group by n.restaurant_id 
    ) n 
    ON r.id = n.restaurant_id LEFT JOIN 
    (select rv.restaurant_id, count(*) as cnt 
     from restaurant_views rv 
     group by rv.restaurant_id 
    ) rv 
    on r.id = rv.restaurant_id 
WHERE r.id = 192 
GROUP BY r.id DESC 
+0

感謝您的輸入! num_likes總是正確的,所以不需要改變。這只是** sum_views **,在第一個查詢 – Tompina 2015-02-24 01:04:57

+0

@Tompa中不正確。 。 。正確執行所有計算並不會造成傷害。 – 2015-02-24 22:32:31

+0

在回答 – Tompina 2015-02-24 23:06:13

0

所以嘗試:

SELECT r.name, date(r.join_date) as join_date, 
(COUNT(DISTINCT a.id) + COUNT(DISTINCT n.id)) as num_likes, 
COALESCE(SUM(rv.views), 0) as sum_views 
FROM restaurants r 
LEFT JOIN apple_likes a ON r.id = a.restaurant_id 
LEFT JOIN android_likes n ON r.id = n.restaurant_id 
INNER JOIN restaurant_views rv on r.id = rv.restaurant_id 
WHERE r.id=192 
GROUP BY r.id DESC; 
0

使用此:

SELECT r.name, date(r.join_date) as join_date, 
    (COUNT(DISTINCT a.id) + COUNT(DISTINCT n.id)) as num_likes, 
    COALESCE(SUM(rv.views), 0) as sum_views 
    FROM restaurants r 
    JOIN apple_likes a ON r.id = a.restaurant_id 
    JOIN android_likes n ON r.id = n.restaurant_id 
    JOIN restaurant_views rv on r.id = rv.restaurant_id 
    WHERE r.id=192 
    GROUP BY r.id DESC; 

請注意,我只是刪除關鍵字left因此使這是一個natural join,所以它不會包括所有的null joins

+0

之前閱讀該問題並沒有害處感謝您的輸入!可悲的是,結果與左前衛一樣 – Tompina 2015-02-24 14:17:05