0

我在 $username 和 $email 被替换的 PHP 变量中编写此代码。我在屏幕上回显了它,它看起来是正确的。我假设 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;
4

1 回答 1

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,因此前两个条件将失败。

于 2013-07-02T19:49:39.910 回答