知道最后的X字段值

I have a database with this table

+------+---------------------+-------------+
| id   | key                 |    value    |
+------+---------------------+-------------+
|  152 | incidencia_0_fecha  |  20150306   |
|  158 | incidencia_1_fecha  |  20150307   | 
|  234 | incidencia_2_fecha  |  20150309   |
|  .   | ......              |  ........   |
|  n   | incidencia_N_fecha  |  date_value |
+------+---------------------+-------------+

And I want to know what is the last key (N its dinamic and i don't know his last value). In this table the last must be incidencia_2_fecha.

How can i do it?

Thanks

You can easily get the number in the string using two REPLACES.

SELECT MAX(
         REPLACE(REPLACE(`key`, 'incidencia_', ''), '_fecha', '')
       )
  FROM mytable

If the values in the id column are strictly increasing, you can do this:

SELECT key FROM your_table WHERE id = (SELECT MAX(id) FROM your_table);

EDIT 1: If the table is quite large, you should make sure that there's an index on the id column. Otherwise the query could take a long time to run.

EDIT 2: Another option, if the value column contains the date at the time the record was inserted (and is indexed), would be to do the above query, but replace id with value, i.e.

SELECT key FROM your_table WHERE value = (SELECT MAX(value) FROM your_table);

First fetch record in desc

SELECT key from tbl_name ORDER BY id DESC LIMIT 0,1