来自外部表的 MySQL 数据


MySQL data from foreign table

我不知道我可以使用哪种方法从MySQL获取数据,因此标题。

无论如何,我有2张桌子event_detailsevent_dates. event_details有字段id,name,created,modified。重要的表是event_dates。其中存储start_dateend_date的事件。

示例数据如下所示

id    eventId    date
1       72       20160520
2       72       20160521
3       72       20160522
4       73       20160518
5       74       20160519
6       75       20160524
7       75       20160525
8       75       20160526

这里startdateenddate的场景是这样的

  1. 如果只有 1 个条目用于eventId则表示事件具有相同的startDateEndDate

  2. 如果 eventId 在表中的次数超过 2 次,则startDate第一个条目,最后一个条目endDate

所以我感到困惑的是,我应该使用什么方法在一个表中获得结果,其中包含事件的startDateendDate

我想要以下格式的数据

Name          startDate     endDate
music show    20160520      20160522
racing show   20160518      20160518
pupet show    20160519      20160519
dance show    20160524      20160526

让我知道在 PHP 文件中获取数组中的数据是否比通过循环运行更容易,或者使用 MySQL 查询更容易。

尝试以下查询:

SELECT
    ed.id,
    ed.name,
    created,
    modified,
    min(startDate) start_date,
    max(endDate) end_date
FROM
    event_details ed
RIGHT JOIN event_dates edates ON ed.id = edates.id
GROUP BY    ed.id;

我想你想要这样的东西:

SELECT t.id,MIN(t.date) as start_date,MAX(t.date) as end_date,t1.name
FROM t,t1
where t.id=t1.id
GROUP BY id