gpt4 book ai didi

mysql - 表行到mysql中的列

转载 作者:行者123 更新时间:2023-11-29 06:16:05 25 4
gpt4 key购买 nike

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

表events_degree:

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

表事件_区域:

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;

结果我得到了所有的行,正如你在图片中看到的那样

picture

我怎样才能避免这种情况,谢谢你的帮助

** 注意 **这是同一个事件,所以它应该为每个事件只显示 1 行问题是我有 3 个多对多关系所以改变的只是日期、学位和地区

最佳答案

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

关于mysql - 表行到mysql中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35862227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com