选择最后一行mysql

时间:2014-10-01 09:52:25

标签: mysql

如何选择最后不同的行?

视频

idvideo name                       
1684    Uno spettacolo da rimanerci
1301    Ciao
1684    Ciamooooo
1823    Prova
1333    Prova2

VIDEOS_VIEWS

idvideo dateview
1684    09-01-2014 11.30
1684    09-01-2014 11.31
1823    09-01-2014 11.31
1684    09-01-2014 11.32
1333    09-01-2014 11.33
1684    09-01-2014 11.34
1333    09-01-2014 11.35

我想要这个结果:

idvideo dateview          name
1333    09-01-2014 11.35  Prova2
1684    09-01-2014 11.34  Ciamooooo
1823    09-01-2014 11.31  Prova

这是查询:

SELECT * 
FROM videos_views  
INNER JOIN videos  
    ON videos_views.idvideo = videos.idvideo 
GROUP BY videos_views.idvideo
ORDER BY MAX(videos_views.dateview)
DESC LIMIT 3

但太低了

2 个答案:

答案 0 :(得分:0)

select VIDEOS_VIEWS.*,VIDEOS.name from VIDEOS LEFT JOIN VIDEOS  ON VIDEOS_VIEWS.idvideo  = VIDEOS.idvideo group by VIDEOS.idvideos DESC limit 3

答案 1 :(得分:0)

您应该将DISTINCT用于此

SELECT DISTINCT vv.idvideo, vv.dateview, v.name FROM videos_views as vv INNER JOIN videos as v ON vv.idvideo = v.idvideo WHERE visibile = '1' GROUP BY vv.idvideo ORDER BY MAX(vv.datavisualizzazione) DESC LIMIT 3