title: MySQL - get recent user visits. display the user only once
body: <p>I have a user_profile_view table:</p>
  
  <p>The definition is as follows:</p>
  
  <pre><code>+------------+------------+------+-----+---------+----------------+
  | Field      | Type       | Null | Key | Default | Extra          |
  +------------+------------+------+-----+---------+----------------+
  | id         | bigint(20) | NO   | PRI | NULL    | auto_increment |
  | user_id    | bigint(20) | NO   | MUL | NULL    |                |
  | viewer_id  | bigint(20) | NO   | MUL | NULL    |                |
  | created_at | datetime   | NO   |     | NULL    |                |
  | updated_at | datetime   | NO   |     | NULL    |                |
  +------------+------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>I'm trying to get the most recent visits but I only want to display the user once</p>
  
  <p>A simple query such as this will return the most recent visits on a profile:</p>
  
  <pre><code> SELECT v.* FROM user_profile_view v 
   WHERE v.user_id != 1 AND v.viewer_id = 1 
   ORDER BY created_at DESC LIMIT 0,10;
  </code></pre>
  
  <p>The result set:</p>
  
  <pre><code>+-----+---------+-----------+---------------------+---------------------+
  | id  | user_id | viewer_id | created_at          | updated_at          |
  +-----+---------+-----------+---------------------+---------------------+
  | 946 |      19 |         1 | 2011-10-05 19:20:19 | 2011-10-05 19:20:19 |
  | 945 |      19 |         1 | 2011-10-05 19:16:15 | 2011-10-05 19:16:15 |
  | 944 |       2 |         1 | 2011-10-05 19:12:02 | 2011-10-05 19:12:02 |
  | 943 |      13 |         1 | 2011-10-05 19:11:45 | 2011-10-05 19:11:45 |
  | 942 |      19 |         1 | 2011-10-05 19:10:41 | 2011-10-05 19:10:41 |
  | 941 |       2 |         1 | 2011-10-05 19:06:35 | 2011-10-05 19:06:35 |
  | 940 |      19 |         1 | 2011-10-05 18:53:04 | 2011-10-05 18:53:04 |
  | 939 |      19 |         1 | 2011-10-05 18:51:56 | 2011-10-05 18:51:56 |
  | 938 |      19 |         1 | 2011-10-05 18:46:59 | 2011-10-05 18:46:59 |
  | 937 |      20 |         1 | 2011-10-05 18:42:35 | 2011-10-05 18:42:35 |
  +-----+---------+-----------+---------------------+---------------------+
  </code></pre>
  
  <p>However I only want to return the user <strong>once</strong>. So if I have recently viewed a users profile 3 times, I only want the most recent visit to that profile and then display the next user who I visited before.</p>
  
  <p>This should be trivial but for some reason I'm stuck.</p>
  
  <p>Thanks folks!</p>