Search code examples
phpmysqlno-duplicates

mySQL joined tables no duplicates


I have two tables: One table contains poetry submitted by members. The other is the member's table. Both tables contain The Member's ID (MID and SubMID). I want to display the last 15 poems that have been updated. However, I want to display ONLY one work for any one author.

This works but it if an author updates a few works then they get displayed many times:

SELECT * FROM submits, members
WHERE submits.SubMID = members.MID AND submits.sub_approved = 'T'
ORDER BY submits.sub_Last_Update DESC LIMIT 15

You can see the results of that query here in the rolling marquee on the right: http://www.Prose-n-Poetry.com

The problem is that one author can take over the marquee by updating a few poems.


Solution

  • SELECT *
    FROM members m
    JOIN (SELECT s.*
          FROM submits s
          JOIN (SELECT SubMID, MAX(sub_Last_Update) lastUD
                FROM submits
                WHERE approved = 'T'
                GROUP BY SubMID) l
          ON s.SubMID = l.SubMID AND s.sub_Last_Update = l.lastUD) s
    ON m.MID = s.SubMID
    ORDER BY s.sub_Last_Update DESC
    LIMIT 15