为什么类似mysql的查询有时无法返回结果?
环境详细信息(XAMPP) Apache/2.4.4(Win32)OpenSSL/1.0.1e PHP/5.5.3 服务器版本:5.6.11-MySQL社区服务器 我有一个搜索栏来搜索与我没有建立关系的新用户。当我添加搜索参数时,有时会得到结果,有时不会得到结果 为什么会出现以下情况 搜索:“Sarah T”返回用户Sarah Testname的信息为什么类似mysql的查询有时无法返回结果?,mysql,stored-procedures,Mysql,Stored Procedures,环境详细信息(XAMPP) Apache/2.4.4(Win32)OpenSSL/1.0.1e PHP/5.5.3 服务器版本:5.6.11-MySQL社区服务器 我有一个搜索栏来搜索与我没有建立关系的新用户。当我添加搜索参数时,有时会得到结果,有时不会得到结果 为什么会出现以下情况 搜索:“Sarah T”返回用户Sarah Testname的信息 call get_contact_list_new(1,"sarahtes") 搜索时:“Sarah测试”不返回任何内容 (u.firstnam
call get_contact_list_new(1,"sarahtes")
搜索时:“Sarah测试”不返回任何内容
(u.firstname + u.lastname) LIKE @args
我使用trim()和str_replace()删除白色字符。即“Sar a h”变成“Sarah”
在数据库中:
firstname | lastname
Sarah | Testname
Robert | Richards
使用以下存储过程:
DROP PROCEDURE IF EXISTS `get_contact_list_new`//
CREATE DEFINER=`root`@`localhost` PROCEDURE `get_contact_list_new`(
IN _id bigint(20),
IN _args varchar(255)
)
BEGIN
IF _args IS NULL THEN
SELECT u.* FROM user_user_relationship AS uur
RIGHT JOIN user AS u
ON uur.user_id_one = _id AND u.user_id = uur.user_id_two
WHERE u.user_id != _id AND uur.status IS NULL;
ELSE
SET @args = CONCAT("%",_args,"%");
SELECT u.* FROM user_user_relationship AS uur
RIGHT JOIN user AS u
ON uur.user_id_one = _id AND u.user_id = uur.user_id_two
WHERE u.user_id != _id AND uur.status IS NULL AND
(u.firstname LIKE @args OR u.lastname LIKE @args OR (u.firstname + u.lastname) LIKE @args OR u.username LIKE @args OR u.email LIKE @args) ;
END IF;
END//
我可以在中间添加随机字符,SARZQTES和Sarah Testname返回。< /P>
这是类似MySQL的查询中的错误吗?还是我的疑问?不管是哪一个,你能解释一下原因吗
这些问题也发生在phpmyadmin mysql控制台上
call get_contact_list_new(1,"saraht")
返回有关Sarah Testname的信息
call get_contact_list_new(1,"sarahtes")
不返回任何内容
(u.firstname + u.lastname) LIKE @args
应该是:
CONCAT(u.firstname, " ", u.lastname) LIKE @args
+
是数字加法,而不是字符串串联。你需要在名字和姓氏之间留一个空格,否则你会得到SarahTestName
,它与%Sarah Testname%不匹配,因为我在args中使用了它。请注意,我认为这有点不同。非常感谢!