无法在连接内获得MAX时间但具有不同的id MySQL

时间:2017-07-16 17:38:00

标签: mysql

我有3个表来获得所需的输出(最新文件路径): -

  1. 事件表[有事件ID]
  2. Event_media表[有事件ID,媒体ID和creation_time]
  3. 媒体表[具有媒体ID和文件路径]
  4.   

    我希望用它们的文件路径获取所有不同的事件,其中事件媒体表的creation_time是MAX,这样我可以使用更新的文件路径包含所有事件

    MySQL查询: -

    select e.event_id, m.filepath, em.creation_time as latest from event e join event_media em on e.event_id=em.event_id join media m on em.media_id = m.media_id where em.creation_time=(select MAX(em.creation_time) from event_media where em.event_id =e.event_id);
    

    输出: -

    +----------+------------------------------------------------------------------------------------+---------------------+
    | event_id | filepath                                                                           | latest              |
    +----------+------------------------------------------------------------------------------------+---------------------+
    |  1000055 | http://localhost:3000/static/images/glasoimage/event1.jpg                          | 2017-07-06 02:06:30 |
    |  1000056 | http://localhost:3000/static/images/glasoimage/event2.jpg                          | 2017-07-06 02:15:15 |
    |  1000058 | http://localhost:3000/static/images/glasoimage/event3.jpg                          | 2017-07-06 02:22:17 |
    |  1000059 | http://localhost:3000/static/uploads/media/upload_b46a5d6f37f1c77a17b87fcbe5ccb975 | 2017-07-06 02:23:17 |
    |  1000066 | http://localhost:3000/static/images/glasoimage/event6.jpg                          | 2017-07-06 17:10:59 |
    |  1000057 | http://localhost:3000/static/uploads/media/upload_12b7a15dfae2ce4f7864c957b8ecf5a6 | 2017-07-06 02:20:52 |
    |  1000062 | http://localhost:3000/static/images/glasoimage/event4.jpg                          | 2017-07-06 16:45:22 |
    |  1000063 | http://localhost:3000/static/images/glasoimage/event5.jpg                          | 2017-07-06 16:47:30 |
    |  1000071 | http://localhost:3000/static/uploads/media/upload_d00ab7878fcf6bacdbf800249678b818 | 2017-07-15 14:10:36 |
    |  1000071 | http://localhost:3000/static/uploads/media/upload_0c85d276316550ef33e2f274635b99c7 | 2017-07-15 23:42:03 |
    |  1000072 | http://localhost:3000/static/images/defaults/default-image.jpg                     | 2017-07-15 23:29:24 |
    |  1000073 | http://localhost:3000/static/images/defaults/default-image.jpg                     | 2017-07-16 12:21:57 |
    |  1000074 | http://localhost:3000/static/images/defaults/default-image.jpg                     | 2017-07-16 12:25:09 |
    |  1000075 | http://localhost:3000/static/images/defaults/default-image.jpg                     | 2017-07-16 13:40:11 |
    |  1000076 | http://localhost:3000/static/images/defaults/default-image.jpg                     | 2017-07-16 13:43:10 |
    |  1000077 | http://localhost:3000/static/images/defaults/default-image.jpg                     | 2017-07-16 14:04:58 |
    |  1000067 | http://localhost:3000/static/images/defaults/default-image.jpg                     | 2017-07-13 21:55:21 |
    |  1000068 | http://localhost:3000/static/uploads/media/upload_d32e3685c22232d00602e827f55f6f64 | 2017-07-13 22:45:04 |
    |  1000069 | http://localhost:3000/static/uploads/media/upload_e0aab7ea8150ee5e7e1f55d98366e51f | 2017-07-13 22:47:50 |
    +----------+------------------------------------------------------------------------------------+---------------------+
    19 rows in set (0.18 sec)
    

    对于第9行和第9行的事件Id 1000071 10输出我有两个旧路径和新路径。

      

    我只希望新的Filepath没有重复的事件ID

    先谢谢

1 个答案:

答案 0 :(得分:1)

您可以在最大

上使用分组依据和联接
  select 
      e.event_id
      , m.filepath
      , em.creation_time as latest 
  from event e 
  join event_media em on e.event_id=em.event_id 
  join media m on em.media_id = m.media_id 
  join ( 
      select 
           event_id
          , MAX(creation_time) as max_time
      from event_media 
      group by event_id
    ) t on t.event_id = em-event_id and em.creation_time = t.max_time