我在PHP变量中编写此代码,其中$ username和$ email被替换。我在屏幕上回应它,它看起来是正确的。我假设MySQL代码存在问题,因为我总是得到的唯一结果是“可接受的”#。有什么帮助吗?
SELECT CASE
WHEN email='$email' THEN '$email is already associated with an account'
WHEN username='$username' THEN '$username is already taken'
ELSE 'acceptable'
END AS result FROM collaborator
UNION
SELECT CASE
WHEN email='$email' THEN '$email is already associated with an account'
WHEN username='$username' THEN '$username is already taken'
ELSE 'acceptable'
END AS result
FROM waitForValidation
LIMIT 1;
答案 0 :(得分:1)
您的问题是在一个或另一个表中无法匹配,因此'acceptable'
来自某个地方。然后,您正在使用union
,这会删除重复项 - 通常是通过排序。尝试以不同的方式进行比赛:
select (case when max(email) = $email then '$email is already associated with an account'
when max(username)='$username' THEN '$username is already taken'
else 'acceptable'
end)
from (select email, username
from collaborator
where email = '$email' or username= '$username'
union
select email, username
from waitForValidation
where email = '$email' or username= '$username'
) t
这使用max()
代替limit
来处理没有匹配的情况。在这种情况下,max()
将返回NULL
,因此前两个条件将失败。