MySQL组由2列组成,但按特定顺序返回

时间:2013-03-29 11:01:46

标签: mysql sql select join greatest-n-per-group

我已经看到类似问题的答案,但无法找到解决此问题的方法。

我需要从三张桌子中选择相册,一张照片和用户详细信息。我只想为每个用户返回最新的专辑。尝试分组和SELECT DISTINCT查询后,我找不到这样做的方法。这是我当前的查询,除了返回来自同一用户的多个专辑外,其工作正常:

SELECT 
gallery_albums.title, gallery_albums.siteID, gallery_albums.albumID, gallery_albums.albumDate, gallery_photos.photoID, gallery_albums.userID
FROM gallery_albums, users, gallery_photos
WHERE users.userID = gallery_albums.userID 
AND gallery_photos.albumID = gallery_albums.albumID
GROUP BY gallery_albums.albumID
ORDER BY gallery_albums.albumDate DESC 
LIMIT 0 , 30

工作代码

这是对aarthi_ram

建议的解决方案的略微修改
SELECT * 
  FROM (
       SELECT gallery_albums.title, gallery_albums.siteID, gallery_albums.albumID, gallery_albums.albumDate, gallery_photos.photoID, gallery_albums.userID, users.fName, users.lName
    FROM gallery_albums, users, gallery_photos
    WHERE users.userID = gallery_albums.userID
    AND gallery_photos.albumID = gallery_albums.albumID
    GROUP BY gallery_albums.albumID
    ORDER BY gallery_albums.albumDate DESC  
  ) AS newTable
  GROUP BY userID
  ORDER BY albumDate DESC
  LIMIT 10

3 个答案:

答案 0 :(得分:0)

很难想象你的表的架构,但我猜测每个表中都有 auto_increment列user可以有多个gallery_albums,也可以包含多个gallery_photos

SELECT  a.*, b.*, c.*
FROM    users a
        INNER JOIN gallery_albums b
            ON a.userID = b.userID 
        INNER JOIN gallery_photos c
            ON b.albumID = c.albumID 
        INNER JOIN
        (
            SELECT  userID, MAX(ID) max_ID
            FROM    gallery_albums
            GROUP   BY userID
        ) d ON  b.userID = d.userID AND
                b.ID = d.max_ID
        INNER JOIN
        (
            SELECT  albumID, MAX(ID) max_ID
            FROM    gallery_photos
            GROUP   BY albumID
        ) e ON  c.albumID = e.albumID AND
                c.ID = e.max_ID
ORDER   BY b.albumDate DESC
LIMIT   0, 30

如您所见,在两个子查询中使用了名为ID的列:de。这些列是每个表上的 auto_incremented column 。如果您有日期列定义其状态,无论它们是否是最近的,您也可以使用它。

答案 1 :(得分:0)

所以,你正在寻找分组最大值。有几种方法可以解决这个问题。在这种情况下,这样的事情应该有效:

SELECT 
gallery_albums.title, gallery_albums.siteID, gallery_albums.albumID, gallery_albums.albumDate, gallery_photos.photoID, gallery_albums.userID
FROM gallery_albums
LEFT JOIN gallery_albums ga2 ON gallery_albums.albumID = ga2.albumID AND gallery_albums.albumDate < ga2.albumDate
JOIN gallery_photos ON gallery_photos.albumID = gallery_albums.albumID
JOIN users ON users.userID = gallery_albums.userID
WHERE ga2.albumID IS NULL
ORDER BY gallery_albums.albumDate DESC 

gallery_albums ga2上的LEFT JOIN与条件ga2.albumID IS NULL相结合,可以保证您只获得最新的专辑。

您可以在MySQL文档中找到two other solutions

答案 2 :(得分:0)

SELECT TOP 1 * FROM (SELECT 
gallery_albums.title, gallery_albums.siteID, gallery_albums.albumID,gallery_albums.albumDate, gallery_photos.photoID, gallery_albums.userID
FROM gallery_albums, users, gallery_photos
WHERE users.userID = gallery_albums.userID 
AND gallery_photos.albumID = gallery_albums.albumID
GROUP BY gallery_albums.albumID
ORDER BY gallery_albums.albumDate DESC 
LIMIT 0 , 30);

可能会奏效。这将有助于从查询生成的结果集中检索第一行。