MySQL - 为“任意数量的空格”添加通配符;在 CONCAT 内?
为什么这不起作用?
SELECT up.first_name, up.last_name
FROM user_profiles up
WHERE u.activated = 1
AND up.first_name LIKE '%" . $search_term . "%'
OR up.last_name LIKE '%" . $search_term . "%'
OR CONCAT(up.first_name, '%', up.last_name) LIKE '%" . $search_term . "%'
GROUP BY up.last_name
ORDER BY up.last_name ASC;
当用户输入 joe
时,我的自动完成功能会返回 joe smith
。 当用户输入 smith
时,我的自动完成功能会返回 joe smith
。
但是当用户输入 joe smith 时,我的自动完成功能返回空白。
我希望 CONCAT
行能够在用户输入 first_name
[中间有任意数量的空格] last_name
时返回结果。
我相信 %
适用于任意数量的字符,但不适用于任意数量的空格。有什么想法如何修复我的代码吗?
谢谢!
Why isn't this working?
SELECT up.first_name, up.last_name
FROM user_profiles up
WHERE u.activated = 1
AND up.first_name LIKE '%" . $search_term . "%'
OR up.last_name LIKE '%" . $search_term . "%'
OR CONCAT(up.first_name, '%', up.last_name) LIKE '%" . $search_term . "%'
GROUP BY up.last_name
ORDER BY up.last_name ASC;
When the user enters joe
my autocomplete returns joe smith
.
When the user enters smith
my autocomplete returns joe smith
.
But when the user enters joe smith
my autocomplete returns blank.
I want the CONCAT
line to be able to return a result in case the user enters first_name
[any number of spaces in between] last_name
.
I believe %
will work for any number of characters, but not for any number of spaces. Any ideas how to fix my code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不起作用,因为您正在
$search_term
中查找文字%
(以及名字和姓氏)替换此行:
用此行:
如果first_name 和last_name 不包含特殊的正则表达式字符,如
$%^[]
等,这将起作用。请参阅; http://dev.mysql.com/doc/refman/5.1/en /regexp.html
请注意,正则表达式不适用于多字节字符集,请在匹配之前将其转换为 latin1。
It doesn't work because you are looking for a literal
%
inside a$search_term
(as well as the first and last names)Replace this line:
with this line:
This will work provided the first_name and last_name do not contain special regex chars like
$%^[]
etc.See; http://dev.mysql.com/doc/refman/5.1/en/regexp.html
Note that regexp does not work with multibyte charsets, convert those to latin1 before matching.