比如说,有个表叫user,其中有2个字段userid,userpass分别保存用户的账号和密码
我在登陆验证的时候可以使用
select * from user where userid='id' and userpass='pass'
来判断。但是这样一来。不能准确的判断出来,到底是账号不存在还是密码错误
有没有一句SQL语句可以完成的方法。我现在使用的是2句SQL,分开判断
最近用户上线率很高了。这样对服务器的性能损耗很大。还望指点
我在登陆验证的时候可以使用
select * from user where userid='id' and userpass='pass'
来判断。但是这样一来。不能准确的判断出来,到底是账号不存在还是密码错误
有没有一句SQL语句可以完成的方法。我现在使用的是2句SQL,分开判断
最近用户上线率很高了。这样对服务器的性能损耗很大。还望指点
解决方案 »
- mysql游标指向字符串结果集,赋值不正确???
- 一个网站的资讯数据表,该如何设计
- 急急急!!!mysql安装在Windows2003上错误error1335.请教
- 简单问题请教,关于postpresql连接问题。。在线等。。。。。。
- the buffer pool是哪个参数控制的值决定的?
- 下面建表的SQL语句,有些地方不知是什么意思,请各位帮忙看一下!
- 关于MySQL的入门问题(在线等待!)!!!
- MySQL4.1.0 for windows中文字符集问题?急!!!
- mysql select update默认会加读锁和写锁吗?innodb模式下
- mysql如何创建表时约定一个字符串有多个不同范围的字符段格式?
- 求一个mysql配置文件,系统情况和要求如下。
- 求一个MYSQL 时间操作函数
测试数据:
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,
只是用户名正确,返回值为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 |
+----------+