1

I have a table that might have one or more rows with the same value for the key_property column.

I want to build a query in MySql that returns a set where every value of key_property is represented for only one of its corresponding rows and that it also receives a key_property filter for a like sentence, chosen by the max value of another column (say, event_id).

How can I achieve that?

UPDATE:

Here's a sample of the non-filtered table:

+--------------+--------+----------+
| key_property | others | event_id |
+--------------+--------+----------+
|    abcd      | B      |     1    |
|    abcd      | A      |     2    |
|    defg      | C      |     3    |
|    abcd      | D      |     4    |
|    hijk      | f      |     4    |
+--------------+--------+----------+

When executing the query with the filter setted as 'd', the result data should look like this:

+--------------+--------+----------+
| key_property | others | event_id |
+--------------+--------+----------+
|    abcd      | D      |     4    |
|    defg      | C      |     3    |
+--------------+--------+----------+
Daniel Calderon Mori
  • 5,466
  • 6
  • 26
  • 36

1 Answers1

0
SELECT * 
FROM tab WHERE (key_property,event_id) IN 
( SELECT key_property, MAX(event_id)
  FROM tab
  WHERE key_property like '%d%'
  GROUP BY key_property
)
Reto
  • 1,305
  • 1
  • 18
  • 32