Afternoon fellow Heapers - I answered
this question using a MySQL 8.0.22 fiddle - the OP specified 5.7, so obviously my answer isn't applicable (I urged the OP to upgrade - see my answer). @Akina 's solution was the accepted one and it was good, simple and elegant.