从多个表中选择最新行

时间:2013-01-14 16:32:20

标签: mysql max

我有多个表的查询,我得到以下结果。然后我想通过t.id分组。我知道我可以简单地通过t.id使用group但是如何选择最新的t.id行,即url tw5.jpg和created_time 10000004

SELECT p.url,t.name,t.num_photo,t.id
FROM photos AS p
LEFT JOIN tag_maps AS tm ON p.id = tm.photo_id
LEFT JOIN tags AS t ON t.id = tm.tag_id

url                       created_time    name      num_photo  id
assets/img/tags/tw1.jpg   1000001         my house  1           1
assets/img/tags/tw2.jpg   1000002         dog       1           2
assets/img/tags/tw3.jpg   1000003         taiwan    2           3
assets/img/tags/tw5.jpg   1000004         taiwan    2           3

这是我通过t.id

使用group时得到的结果
url                       created_time    name      num_photo  id
assets/img/tags/tw1.jpg   1000001         my house  1           1
assets/img/tags/tw2.jpg   1000002         dog       1           2
assets/img/tags/tw3.jpg   1000003         taiwan    2           3

这就是我想要的

url                       created_time    name      num_photo  id
assets/img/tags/tw1.jpg   1000001         my house  1           1
assets/img/tags/tw2.jpg   1000002         dog       1           2
assets/img/tags/tw5.jpg   1000004         taiwan    2           3

EDITED

照片表

id    url                      created_time
1     assets/img/tags/tw1.jpg  1000001
2     assets/img/tags/tw2.jpg  1000002
3     assets/img/tags/tw3.jpg  1000003
4     assets/img/tags/tw5.jpg  1000004

标签表

id name     num_photo
1  my house 1
2  dog      1
3  taiwan   2

Tag_maps

id   tag_id   photo_id
1    1        1
2    2        2
3    3        3
4    3        4

1 个答案:

答案 0 :(得分:1)

编辑#2:没有看到每个表或表结构中的样本数据,我猜测以下内容将起作用:

SELECT p.url,
  created_time,
  t.name,
  t.num_photo,
  t.id
FROM photos p
LEFT JOIN tag_maps AS tm 
    ON p.id = tm.photo_id
LEFT JOIN tags AS t 
    ON t.id = tm.tag_id
INNER JOIN
(
  select max(created_time) MaxDate, t.id
  FROM photos p
  LEFT JOIN tag_maps AS tm 
      ON p.id = tm.photo_id
  LEFT JOIN tags AS t 
      ON t.id = tm.tag_id
  group by t.id
) d
  on p.created_time = d.MaxDate
  and t.id = d.id;

请参阅SQL Fiddle with Demo

或者另一种方法是使用子查询从max(photo_id)表中返回tag_Id tag_maps并在结合中使用该结果:

SELECT p.url,
  created_time,
  t.name,
  t.num_photo,
  t.id
FROM photos p
INNER JOIN
(
  select max(photo_id) photo_id, tag_id
  from tag_maps
  group by tag_id
) AS tm 
    ON p.id = tm.photo_id
LEFT JOIN tags AS t 
    ON t.id = tm.tag_id

请参阅SQL Fiddle with Demo

结果是:

|                     URL | CREATED_TIME |     NAME | NUM_PHOTO | ID |
----------------------------------------------------------------------
| assets/img/tags/tw1.jpg |      1000001 | my house |         1 |  1 |
| assets/img/tags/tw2.jpg |      1000002 |      dog |         1 |  2 |
| assets/img/tags/tw5.jpg |      1000004 |   taiwan |         2 |  3 |

<击> 编辑#1因为您的网址不同而您想要max(id),那么您应该可以使用:

SELECT p.url,
  t.name,
  t.num_photo,
  Max(t.id) id
FROM photos p
LEFT JOIN tag_maps AS tm 
    ON p.id = tm.photo_id
LEFT JOIN tags AS t 
    ON t.id = tm.tag_id
group by t.name, t.num_photo  

OP:您可以使用子查询:

SELECT p.url,
    t.name,
    t.num_photo 
FROM
(
    select MAX(created_time) created_time, url, id
    from photos
    group by url, id
)  AS p
LEFT JOIN tag_maps AS tm 
    ON p.id = tm.photo_id
LEFT JOIN tags AS t 
    ON t.id = tm.tag_id

如果id表中的photos值对于每一行都是唯一的,那么您可能需要使用以下内容:

SELECT p1.url,
    t.name,
    t.num_photo 
FROM photos p1
inner join
(
    select MAX(created_time) created_time, url
    from photos
    group by url
)  AS p2
    on p1.url = p2.url
    and p1.created_time = p2.created_time
LEFT JOIN tag_maps AS tm 
    ON p1.id = tm.photo_id
LEFT JOIN tags AS t 
    ON t.id = tm.tag_id

<击>