Fixing MySQL query issues