Jump to content

Query only most recent timestamp based on two other distinct columns


AnalyzeThis

Recommended Posts

Here is the database (basically ;) )

===================

| name | type | timestamp

====================

| Brain | Blue Car | 1/2/2012

| Brain | Blue Car | 2/2/2012

| Brain | Red Car | 3/2/2013

| Fred | Blue Car | 2/2/2012

| Fred | Red Car | 2/2/2012

| Fred | Red Car | 1/2/2010

| Brenda | Red Car | 2/2/2012

| Wendy | Red Car | 1/2/2013

 

I want to return a list where only the distinct name and type is returned with only the most recent timestamp e.g.

 

===================

| name | type | timestamp

====================

| Brain | Blue Car | 2/2/2012

| Brain | Red Car | 3/2/2013

| Fred | Blue Car | 2/2/2012

| Fred | Red Car | 2/2/2012

| Brenda | Red Car | 2/2/2012

| Wendy | Red Car | 1/2/2013

 

I do not want to delete any old records or create a new table. I just want to run a query within php to obtain this. I'm a little brain dead at the moment and googling isn't providing much use.

 

Thank you!

I wanted to make certain this seemed to of work:

 

$DB02->Query("SELECT `a`,`b`.`ID` AS 'c' , `d` , `e` , `f` , DATE_FORMAT( max(`g`), '%m/%d/%Y' ) AS `date`

FROM `h` LEFT JOIN `i` ON `a`.`b` = `x`.`ID`

WHERE `a` = '{$x["ID"]}' AND y = 1 GROUP BY `a`, `b` ORDER BY `c` ASC LIMIT {$set_limit}, {$limit}");

  Quote

$DB02->Query("SELECT `a`,`b`.`ID` AS 'c' , `d` , `e` , `f` , DATE_FORMAT( max(`g`), '%m/%d/%Y' ) AS `date`

FROM `h` LEFT JOIN `i` ON `a`.`b` = `x`.`ID`

WHERE `a` = '{$x["ID"]}' AND y = 1 GROUP BY `a`, `b` ORDER BY `c` ASC LIMIT {$set_limit}, {$limit}");

 

What has that query got to do with your original post with a single table with three columns?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.