2016-02-14 205 views
0

我在MySQL查询,做一些认证检查,但它是相当长的。我想知道是否会有另一种方法来缩短/简化它,但实际上所有的子句都是必需的。是否有更简单的方法来编写此查询?

select nickname from nicknames where nickname = '$nick' and password = '$md5pass' 
union 
select '$nick' as nickname from nicknames AS t1 where not 
    (nickname = '$nick' and password = '$md5pass') 
and not exists 
    (select 1 from nicknames where nickname = '$nick') 

回答

1

这个怎么样?

select coalesce(max(nickname), '$nick') 
from nicknames 
where nickname = '$nick' and password = '$md5pass'; 
+0

这不起作用一样。当没有提供密码但是昵称存在时,它会返回一行。 – user5603796

相关问题