-2

I'm very new to MySQL and PHP, and I want to make my page tell me the newest entry for each name, but not just the newest entry for the table.

The table looks something like this,

55 | Curtis | present | 2014-06-22
87 | Curtis | present | 2014-06-22
56 | James  | absent  | 2014-08-25
57 | Curtis | late    | 2014-08-25
48 | Will   | present | 2014-08-25
47 | James  | present | 2014-08-18
43 | Will   | present | 2014-08-18

I want it to output like this,

43 | Will   | present | 2014-08-25
47 | James  | present | 2014-08-25
57 | Curtis | late    | 2014-08-25

Would anybody be able to help me?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    [Have you tried anything](http://whathaveyoutried.com)? – Barranka Sep 01 '14 at 00:42
  • You may need to ask this in another site of the network, http://dba.stackexchange.com/ as this site is for programming issues. – canolucas Sep 01 '14 at 00:43
  • I have absolutely no idea how I would do it. I would like to get it so that MySQL does the processing, but if I have to do it in PHP, ok. – Curtis Alcock Sep 01 '14 at 00:44
  • @MosheKatz You're probably right, will read that. – Curtis Alcock Sep 01 '14 at 00:45
  • @CurtisAlcock shouldn't the latest for James be `absent | 2014-08-25` and for Will be `present | 2014-08-25` or what is the other criteria you're using the entry id? if id also plays a role then Curtis would be wrong. – Prix Sep 01 '14 at 00:47
  • @CurtisAlcock You should take the time to look for a good SQL tutorial. Google around a bit, there are lots of 'em – Barranka Sep 01 '14 at 00:52

1 Answers1

0

Assuming your id is incremental (that is, more recent records have bigger id values), this can be solved by using a nested query that gets the max values of id for each name:

select a.*
from yourTable as a
     inner join (
         select max(id) as max_id 
         from yourTable 
         -- Add any WHERE conditions here (for example: WHERE status='present')
         group by name
     ) as b on a.id = b.max_id
-- WHERE conditions go here
-- ORDER BY columns go here
Barranka
  • 20,547
  • 13
  • 65
  • 83
  • @MosheKatz Because I see the posibility that a single `name` can have more than one record in a single day. Given the (very) limited info provided by the OP, I'd like to "play it safe". – Barranka Sep 01 '14 at 00:48