我的应用程序当前连接到MySQL并执行以下查询:
select nickname
from users
where nickname = '$nick' and password = '$pass'
$nick
和$pass
由应用程序插入。我需要提前执行以下查询:
如果
where nickname = '$nick' and password = '$pass'
返回一行,则照常返回该行。如果
where nickname = '$nick' and password = '$pass'
返回0行,我需要它检查'$nick'
是否存在,并且事实是'$pass'
错误的是存在0行,如果是这种情况,则返回0行,其他明智的做法是,如果表中没有'$nick'
,则无论如何都要返回该行。也就是说,如果昵称在表格中并且密码正确,或者昵称不在表格中,请返回一行。否则,如果表中有昵称且密码不正确,则返回0行。
使用MySQL是否可能?
最佳答案
尝试这个:
select nickname
from users
where nickname = '$nick' and password = '$pass'
union
select '$nick' as nickname
from users AS t1
where not (nickname = '$nick' and password = '$pass') and
not exists (select 1
from users
where nickname = '$nick')
Demo here
关于mysql - MySQL CASE/IF用法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35382698/