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!

Link to comment
Share on other sites

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}");

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.