MySQL - select rows under an ID, group by column value that has the latest timestamp
<p>Table:</p>

<pre><code>----------------------------------------------------
ID | field_name | field_value | timestamp
----------------------------------------------------
2  | postcode   | LS1         | 2016-11-09 16:45:15
2  | age        | 34          | 2016-11-09 16:45:22
2  | job        | Scientist   | 2016-11-09 16:45:27
2  | age        | 38          | 2016-11-09 16:46:40
7  | postcode   | LS5         | 2016-11-09 16:47:05
7  | age        | 24          | 2016-11-09 16:47:44    
</code></pre>

<p>I wonder if anyone could give me a few pointers, based on the above data, I would like to query by ID 2, return a row for each unique field_name (if more than one row exists under the same id with the same field_name then just return the row with the latest timestamp).</p>

<p>I have managed to almost achieve this by grouping the field_name, which will return a list of unique rows but not necessarily the latest row.</p>

<pre><code>SELECT * FROM fragment WHERE (id = :id) GROUP BY field_name
</code></pre>

<p>I would really be grateful for any pointers on what exactly I should do here, and how I could fit something along the lines of MAX(timestamp) in this query,</p>

<p>Many thanks!</p>