Adjusted query so that it works in MySQL