1

Say I have a database similar to the following:

Table events_degree:

event_id     degree_id
    1           1
    1           31
  ...           ...

Table events_area:

event_id     area_id
    1           1
    1           31
  ...           ...

Table events_schedule:

event_id     schedule
    1          Time 1
    1          Time 2
  ...           ...

Table events:

   id           name
    1           prom
    2         homecoming
  ...           ...

Table degree:

    id          name           shortened
    1     computer science        cs
    2      something else         se
  ...           ...              ...

Table area:

    id          name
    1           hall 1
    2            gym
  ...           ...

Table building:

    id          name
    1         main building
    2        second building
  ...           ...

What i want to do it's merge the columns since i have this query

SELECT `e`.*, `eh`.`start_date`, `eh`.`end_date`, `c`.`name` AS `degree_name`, `c`.`shortened` AS `shortened_degree`, `ai`.`name` AS `area_name`, `ed`.`name` AS `building_name` 
FROM `events` `e` 
LEFT JOIN `event_schedule` `eh` ON `e`.`id` = `eh`.`event_id` 
LEFT JOIN `event_degree` `ec` ON `e`.`id` = `ec`.`event_id` 
LEFT JOIN `degree` `c` ON `c`.`id` = `ec`.`degree_id` 
LEFT JOIN `event_area` `ea` ON `e`.`id` = `ea`.`event_id` 
LEFT JOIN `area` `ai` ON `ai`.`id` = `ea`.`area_id` 
LEFT JOIN `building` `ed` ON `ed`.`id` = `ai`.`building_id` 
WHERE `e`.`active` = '1' 
AND `eh`.`start_date` >= '2016-03-08' 
AND `eh`.`end_date` < '2016-07-01' 
ORDER BY `eh`.`fecha_inicio` ASC;

and as a result i get all the rows "duplicated" as you can see in the picture

picture

how can i avoid this, thanks for your help

** note ** it's the same event so it should be displaying only 1 row for every event the problem is that i has 3 many to many relationships so the things that change are only dates, degrees and areas

Cesar Mtz
  • 322
  • 2
  • 13

2 Answers2

2

You can use GROUP BY

SELECT DISTINCT `e`.*, `eh`.`start_date`, `eh`.`end_date`, `c`.`name` AS `degree_name`, `c`.`shortened` AS `shortened_degree`, `ai`.`name` AS `area_name`, `ed`.`name` AS `building_name` 
FROM `events` `e` 
LEFT JOIN `event_schedule` `eh` ON `e`.`id` = `eh`.`event_id` 
LEFT JOIN `event_degree` `ec` ON `e`.`id` = `ec`.`event_id` 
LEFT JOIN `degree` `c` ON `c`.`id` = `ec`.`degree_id` 
LEFT JOIN `event_area` `ea` ON `e`.`id` = `ea`.`event_id` 
LEFT JOIN `area` `ai` ON `ai`.`id` = `ea`.`area_id` 
LEFT JOIN `building` `ed` ON `ed`.`id` = `ai`.`building_id` 
WHERE `e`.`active` = '1' 
AND `eh`.`start_date` >= '2016-03-08' 
AND `eh`.`end_date` < '2016-07-01'
GROUP BY `your_id`;
Jay Doshi
  • 666
  • 1
  • 5
  • 16
0

Use DISTINCT while selecting

SELECT DISTINCT `e`.*, `eh`.`start_date`, `eh`.`end_date`, `c`.`name` AS `degree_name`, `c`.`shortened` AS `shortened_degree`, `ai`.`name` AS `area_name`, `ed`.`name` AS `building_name` 
FROM `events` `e` 
LEFT JOIN `event_schedule` `eh` ON `e`.`id` = `eh`.`event_id` 
LEFT JOIN `event_degree` `ec` ON `e`.`id` = `ec`.`event_id` 
LEFT JOIN `degree` `c` ON `c`.`id` = `ec`.`degree_id` 
LEFT JOIN `event_area` `ea` ON `e`.`id` = `ea`.`event_id` 
LEFT JOIN `area` `ai` ON `ai`.`id` = `ea`.`area_id` 
LEFT JOIN `building` `ed` ON `ed`.`id` = `ai`.`building_id` 
WHERE `e`.`active` = '1' 
AND `eh`.`start_date` >= '2016-03-08' 
AND `eh`.`end_date` < '2016-07-01' 
ORDER BY `eh`.`fecha_inicio` ASC;
FallAndLearn
  • 4,035
  • 1
  • 18
  • 24