title: MySql Select statement distinct latest date value based on Primary ID of Keyword
body: <p>Basically I have a list of SEO keywords in one table and their Ranking including Date entered in another table.</p>
  
  <h2>keywords</h2>
  
  <pre><code>[keywordID(PKID)] [keyword]
  ----------------------------
  1                 SEO
  2                 Vancouver Homes
  </code></pre>
  
  <h2>keywordRanking</h2>
  
  <pre><code>[rankingID(PKID)] [keywordID(FKID)] [ranking] [rankingDate(timestamp)]
  ----------------------------------------------------------------------
  1                 1                 3         Jan 01, 2010
  2                 1                 5         Jan 02, 2010
  3                 1                 8         Jan 03,2010
  4                 2                 4         Jan 01, 2010
  </code></pre>
  
  <p>Here are the results I want. I want the latest ranking of a keyword based on its latest ranking Date without duplicates.</p>
  
  <p>For the data above, it should return:</p>
  
  <pre><code>                  [keywordID] [rankingID] [ranking] [rankingDate] 
                    -----------------------------------------------
   SEO              1           3           8         Jan 03, 2010
   Vancouver Homes  4           2           4         Jan 01, 2010
  </code></pre>
  
  <p>What would the statement be to select the latest ranking for a keyword? Right now I have it returning duplicate rows for the keywordID.</p>
  
  <p>Please ask if more information is required here is my current sql statement.</p>
  
  <pre><code>SELECT * 
    from keywords,
         keywordRankings 
   WHERE keywords.keywordID = keywordRankings.keywordID;
  </code></pre>
  
  <p>My current solution which works for me is...</p>
  
  <pre><code>SELECT s1.keywordID, keyword, rankingDate, ranking
  FROM keywords s1, keywordRankings
  WHERE keywordRankingRecord = (
  SELECT MAX( s2.keywordRankingRecord )
  FROM keywordRankings s2
  WHERE s1.keywordID = s2.keywordID )     
  </code></pre>