2017-04-11 52 views
3

我有三個表,我想統計每個表中每個度假村的記錄數。我得到了一個我無法解釋的意外結果。外部聯接沒有給出預期的結果

我的表如下:

CREATE TABLE `game_items` (
    `id_items` int(11) NOT NULL, 
    `id_resort` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
INSERT INTO `game_items` (`id_items`, `id_resort`) VALUES 
(36, 81), 
(38, 81), 
(39, 67); 

CREATE TABLE `game_slopes` (
    `id_slopes` int(11) NOT NULL, 
    `id_resort` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
INSERT INTO `game_slopes` (`id_slopes`, `id_resort`) VALUES 
(16, 81); 

CREATE TABLE `game_staff` (
    `id_staff` int(11) NOT NULL, 
    `id_resort` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
INSERT INTO `game_staff` (`id_staff`, `id_resort`) VALUES 
(1, 69), 
(3, 67), 
(5, 81), 
(7, 81), 
(8, 81), 
(12, 81); 

CREATE TABLE `game_resorts` (
    `id_resort` int(11) NOT NULL, 
    `id_player` int(11) DEFAULT NULL, 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
INSERT INTO `game_resorts` (`id_resort`, `id_player`) VALUES 
(66, 59), 
(67, 60), 
(68, 61), 
(69, 62), 
(70, 63), 
(81, 67), 
(82, 68); 

而且我的查詢:

SELECT `game_players_tbl`.`id_player`, `game_resorts`.`id_resort`, 
COUNT(game_items_tbl.id_items) as item_count, 
COUNT(game_slopes_tbl.id_slopes) as slope_count, 
COUNT(game_staff_tbl.id_staff) as staff_count 
FROM `game_resorts` 
INNER JOIN `game_players` as `game_players_tbl` ON `game_resorts`.`id_player` = `game_players_tbl`.`id_player` 
LEFT OUTER JOIN `game_items` as `game_items_tbl` ON `game_resorts`.`id_resort` = `game_items_tbl`.`id_resort` 
LEFT OUTER JOIN `game_slopes` as `game_slopes_tbl` ON `game_resorts`.`id_resort` = `game_slopes_tbl`.`id_resort` 
LEFT OUTER JOIN `game_staff` as `game_staff_tbl` ON `game_resorts`.`id_resort` =`game_staff_tbl`.`id_resort` 
GROUP BY `game_resorts`.`id_resort` 
ORDER BY `game_resorts`.`reputation` DESC 

結果是:

id_player id_resort item_count slope_count  staff_count  
61 68 0 0 0 
63 70 0 0 0 
67 81 8 8 8 
68 82 0 0 0 
62 69 0 0 1 
59 66 0 0 0 
60 67 1 0 1 

但我希望:

id_player id_resort item_count slope_count  staff_count  
61 68 0 0 0 
63 70 0 0 0 
67 81 2 1 4 
68 82 0 0 0 
62 69 0 0 1 
59 66 0 0 0 
60 67 1 0 1 

我不明白爲什麼我在度假村ID 81的每次計數中得到8次。我試過不同的選擇,但從來沒有得到正確的結果。

編輯:新增game_resorts

+0

你可以添加game_resorts的數據,請說? –

+0

測試它,我將添加它作爲答案。當然,解釋爲什麼 –

+0

不幸的是,我的結果與你的改變相同。我在原來的帖子中添加了我的表 – remyremy

回答

1

你的主要問題是你的表game_items先後爲game_resorts倍數記錄。這會導致您複製連接到game_resorts表的所有數據。正如@Jorge Campos所說,最好爲每個表創建單獨的計數,然後將它們加入到度假村表中。

SQL查詢

SELECT `game_players_tbl`.`id_player`, `game_resorts`.`id_resort`, 
game_items_tbl.Count AS item_count, 
game_slopes_tbl.Count AS slope_count, 
game_staff_tbl.Count AS staff_count 
FROM `game_resorts` 
INNER JOIN `game_players` AS `game_players_tbl` ON `game_resorts`.`id_player` = `game_players_tbl`.`id_player` 
LEFT OUTER JOIN (
    SELECT `game_items`.`id_resort`, COUNT(`game_items`.`id_items`) AS Count FROM `game_items` GROUP BY `game_items`.`id_resort` 
) AS `game_items_tbl` ON `game_resorts`.`id_resort` = `game_items_tbl`.`id_resort` 
LEFT OUTER JOIN (
    SELECT `game_slopes`.`id_resort`, COUNT(`game_slopes`.`id_slopes`) AS Count FROM `game_slopes` GROUP BY `game_slopes`.`id_resort` 
) AS `game_slopes_tbl` ON `game_resorts`.`id_resort` = `game_slopes_tbl`.`id_resort` 
LEFT OUTER JOIN (
    SELECT `game_staff`.`id_resort`, COUNT(`game_staff`.`id_staff`) AS Count FROM `game_staff` GROUP BY `game_staff`.`id_resort` 
) AS `game_staff_tbl` ON `game_resorts`.`id_resort` =`game_staff_tbl`.`id_resort` 
GROUP BY `game_resorts`.`id_resort` 
ORDER BY `game_resorts`.`reputation` DESC 

編輯:而不是懶惰,我繼續進行計數所有剩餘表。

編輯:固定的最後子查詢到正確的表由@remyremy

+0

現在測試此 – remyremy

+0

工作,謝謝!最後一次加入結束時的小錯字:'GROUP BY game_slopes'應該是'GROUP BY game_staff'。我原來的代碼是使用PHP和ActiveRecord,有沒有簡單的方法可以直接將上面的SQL轉換成ActiveRecord查詢?在線轉換器或其他... – remyremy

+0

沒有足夠的PHP經驗,也許別人可以幫助你。 –

相關問題