How to get last inserted rows where a condition is satisfied
If you want the 'last' A:

SELECT * FROM table2
    where esp = 'A'
    ORDER BY id DESC
    LIMIT 1;


If you need something more complicated, see "groupwise-max".