比如说,有个表叫user,其中有2个字段userid,userpass分别保存用户的账号和密码
我在登陆验证的时候可以使用
select * from user where userid='id' and userpass='pass'
来判断。但是这样一来。不能准确的判断出来,到底是账号不存在还是密码错误
有没有一句SQL语句可以完成的方法。我现在使用的是2句SQL,分开判断
最近用户上线率很高了。这样对服务器的性能损耗很大。还望指点

解决方案 »

  1.   

    这个我觉得一般都是需要2次来判断的吧.下面是我按照楼主的一个sql 来写的 不知道理解的正确不:
    测试数据:
    mysql> select * from user;
    +--------+----------+
    | userid | userpass |
    +--------+----------+
    | asd    | 123456   |
    | asd123 | asdqq    |
    +--------+----------+我的帐号是:asd 密码:123456 
    mysql> select *,locate('asd',userid) as is_userid,locate('123456',userpass) as i
    s_userpass from user;
    +--------+----------+-----------+-------------+
    | userid | userpass | is_userid | is_userpass |
    +--------+----------+-----------+-------------+
    | asd    | 123456   |         1 |           1 |
    | wer    | asdqq    |         0 |           0 |
    +--------+----------+-----------+-------------+
    后面2个附加字段表示 is_userid(帐号):1:正确,0:错误,is_userpass(密码):1:正确,0:错误,
    ...
    上面的很牵强,觉得还是2句sql的好.楼下的大牛继续想
      

  2.   

    用一个SQL语句可以搞定,
    如果用户名及密码都正确,返回值为2,
    只是用户名正确,返回值为1
    用户名都错误,返回值则为0。
    请看下例,设正确的用户名为abc,密码为abcd.
    mysql> select count(*) from (select 1 from user where userid='abc' union select 2 from user where userid='abc' and userpass='abcd');
    ERROR 1248 (42000): Every derived table must have its own alias
    mysql> select count(*) from (select 1 from user where userid='abc' union select 2 from user where userid='abc' and userpass='abcd') a;
    +----------+
    | count(*) |
    +----------+
    |        2 |
    +----------+
    1 row in set (0.08 sec)mysql> select count(*) from (select 1 from user where userid='abc' union select 2 from user where userid='abc' and userpass='abcde') a;
    +----------+
    | count(*) |
    +----------+
    |        1 |
    +----------+
    1 row in set (0.00 sec)mysql> select count(*) from (select 1 from user where userid='abc1' union select 2 from user where userid='abc1' and userpass='abcde') a;
    +----------+
    | count(*) |
    +----------+
    |        0 |
    +----------+