Tightened up SQL conditions to prevent "ambiguous clause" errors in MySQL