表行在mysql

时间:2016-03-08 08:16:22

标签: mysql sql duplicates multiple-columns rows

假设我有一个类似于以下内容的数据库:

表events_degree:

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

表events_area:

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

表events_schedule:

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

表事件:

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

表学位:

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

表区:

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

桌面建设:

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

我想要做的就是合并列,因为我有这个查询

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;

因此我获得所有行&#34;重复&#34;正如你在图片中看到的那样

picture

我可以如何避免这种情况,谢谢你的帮助

** note ** 它是同一个事件所以它应该只为每个事件显示1行问题是我有3对多关系 所以改变的事情只是日期,学位和领域

2 个答案:

答案 0 :(得分:2)

您可以使用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`;

答案 1 :(得分:0)

选择

时使用DISTINCT
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;