How to filter out the records with the latest date? I have a table with id, task_id, created_taskaction (= rec.cr.date).

I would like to get the latest record for e.g. task_id 0001.

In the wordpress framework I tried this:

&gt; $last_task_action2=$wpdb-&gt;get_row("SELECT * FROM $wpdb-&gt;task_actions
&gt; NATURAL JOIN ( SELECT id_task, MAX(created_taskaction) AS datetime
&gt; FROM $wpdb-&gt;task_actions GROUP BY id_task) mostrecent ON
&gt; $wpdb-&gt;task_actions.id_task = $wpdb-&gt;mostrecent.id_task  AND
&gt; $wpdb-&gt;task_actions.datetime = $wpdb-&gt;mostrecent.datetime");


But it doesn't work. I tried copying from this thread, but it's still confusing:
https://stackoverflow.com/questions/612231/how-can-i-select-rows-with-maxcolumn-value-distinct-by-another-column-in-sql

Even these two don't show ANY data...

$last_task_actiond = $wpdb-&gt;get_row("SELECT MAX (created_taskaction) AS 'Max' FROM $wpdb-&gt;task_actions");
echo $last_task_actiond;


and

$last_task_actiond = $wpdb-&gt;get_row("SELECT MAX (created_taskaction) AS 'Max' FROM wp_task_actions");
echo $last_task_actiond;


Nothing shows up...
It worked I found 2 bugs:

Line 5:


  $wpdb->task_actions.datetime = $wpdb->mostrecent.datetime");
  Had to be: task_actions.created_taskaction instead of task_actions.datetime 


Also the $wpdb-&gt; sometimes doesn't work properly, so I changed all of them with wp_