外部联接未给出预期结果

时间:2017-04-11 20:15:55

标签: mysql codeigniter join

我有三个表,我想计算每个表中每个度假村的记录数。我得到了一个我无法解释的意外结果。

我的表格如下:

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

1 个答案:

答案 0 :(得分:1)

您遇到的主要问题是您的表struct person { char sex; int age; struct person *prev, *next; }; struct person* first = NULL; static void add_person(void) { struct person* temp = (struct person*)malloc(sizeof(struct person)); if (temp == NULL) printf("Unable to allocate memory"); { printf("Enter the gender: "); scanf("%c", person->sex); printf("Enter the age: "); scanf("%d", person->age); if(first==NULL){ temp->prev=NULL; temp->next=NULL; first=temp; } else{ temp->prev=NULL; temp->next=first; first->prev=temp; first=temp; } free(temp); } static void print(void) { struct person* present = first; while(present != NULL){ printf("%c", present->sex); printf("%i", present->age); present=present->next; } 具有game_items的倍数记录。这导致您复制加入game_resorts表的所有数据。正如@Jorge Campos所说,最好为每个表创建单独的计数,然后将它们加入到您的度假村表中。

SQL查询

game_resorts

编辑:我没有懒惰,而是继续对所有剩余的表进行计数。

编辑:修复了@remyremy

所述的修正表的最后一个子查询