Fixing issue with query mysql < 5.5