-1

How do I get the latest ID from the GROUP BY?

Right now, I made this:

SELECT * FROM (SELECT * FROM mycp GROUP BY ttype ORDER BY MAX(ID) DESC LIMIT 10) mycp WHERE PlayerName = 'Mark_Fletcher' OR fbk = 14 ORDER BY ID ASC

The output will be as show below.

The current table example:

==========================================
|  ID       |       ttype        | name
==========================================
|   1       |  Business 1      | John
|   2       |  Business 2      | Peter
|   3       |  Business 3      | Steward
|   4       |  Business 4      | Ethan
|   5       |  Business 1      | Bob
|   6       |  Business 1      | Patrick
==========================================

The current output:

==========================================
|  ID       |       ttype        | name
==========================================
|   1       |  Business 1      | John
|   2       |  Business 2      | Peter
|   3       |  Business 3      | Steward
|   4       |  Business 4      | Ethan
==========================================

What I wanted is this:

==========================================
|  ID       |       ttype        | name
==========================================
|   2       |  Business 2      | Peter
|   3       |  Business 3      | Steward
|   4       |  Business 4      | Ethan
|   6       |  Business 1      | Patrick //this become the latest Business 1
==========================================
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

1 Answers1

0
SELECT m1.* 
FROM mycp m1
JOIN 
(  -- below subquery will pick max id
  SELECT MAX(ID) as m_id
  FROM mycp 
  GROUP BY ttype
) m2 on m1.id = m2.m_id -- join ensure to pick max id
WHERE m1.PlayerName = 'Mark_Fletcher' 
OR m1.fbk = 14 
ORDER BY m1.ID ASC
Koushik Roy
  • 6,868
  • 2
  • 12
  • 33
juergen d
  • 201,996
  • 37
  • 293
  • 362