本文介绍了在mysql中使用join时,从右表中选择最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试联接两个表,并希望从第二个表获取最新记录.

Trying to join two tables and want to get latest record from second.

SELECT * 
FROM gallery 
    LEFT JOIN images_videos ON gallery.ID=images_videos.gallery_id 
WHERE gallery.user_id ="+another_userid+" 
GROUP BY gallery.ID 
ORDER BY gallery.ID DESC

通过此查询,我从images_videos获得了最早的图像但我想从images_videos表中选择最新图像请帮忙我在images_videos画廊表中有多个条目

via this query i got the oldest images from images_videosbutI want to select latest images from images_videos tablePlease helpI have multiple entries in images_videos for gallery table

我想要每个画廊的最新图片.

i want latest image for each gallery.

推荐答案

暗示您的images_videos表具有ID字段

implied your images_videos table has an ID field

SELECT * 
FROM   gallery g1 
       LEFT JOIN images_videos iv1 
              ON g1.id = iv1.gallery_id 
WHERE  gallery.user_id = "+another_userid+" 
       AND iv1.id = (SELECT Max(id) 
                     FROM   images_videos iv2 
                     WHERE  iv1.gallery_id = iv2.gallery_id) 

这篇关于在mysql中使用join时,从右表中选择最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 10:40