Isnt last IP tyhe IP the player is currently logged in with?
Instead of account ID, you could pass the session pointer to the account hooks giving access to, among other things, the IP string. Then you wouldnt need the query at all.

This would also apply to the other query done on login attempt. Since failed login attempt IP is not saved in memory, it cant be accessed atm though so that needs a query

WorldSession::GetRemoteAddress()