My table payment_status have these fields:
- id
- payment_id
- status
- created
- created_by
Many entries could have the same payment_id… So, I want to get the last status for one payment_id…
I have this request that work but take too much time to load… I would like to have an optimize version to group by payment_id and take the last status.
SELECT pstatus.*
FROM `payment_status` AS pstatus
WHERE pstatus.id = (
SELECT id
FROM `payment_status`
WHERE pstatus.status = '200'
ORDER BY created DESC
LIMIT 1
)
GROUP BY pstatus.payment_id
ORDER BY pstatus.payment_id DESC
Try this query –
…then add WHERE conditions you need.