简体   繁体   English

MySQL的喜欢查询不拉相似的数据

[英]MySQL LIKE query not pulling similar data

I have a simple MySQL-based search script, I will post only the part relevant to the problem below. 我有一个简单的基于MySQL的搜索脚本,我将仅在下面发布与该问题有关的部分。

$name = 'John Doe';
$query = 'SELECT username FROM members WHERE name LIKE %$name%';

Now, the problem is when I search for John Doe instead of getting the user with that particular name, I get all users named John and Doe and, the funny thing is that, it does not even put John Doe at the top, meaning... you could find John xxxx before that. 现在的问题是,当我搜索John Doe而不是获得具有该特定名称的用户时,我得到了所有名为JohnDoe用户,有趣的是,它甚至没有将John Doe 。 ..您可以在此之前找到John xxxx

Is there and advanced MySQL attribute to accomplish this task? 是否有高级MySQL属性来完成此任务?

change 更改

$query = 'SELECT username FROM members WHERE name LIKE %$name%';

to

$query = 'SELECT username FROM members WHERE name LIKE "'.%$name%.'"';

or 要么

$query = "SELECT username FROM members WHERE name LIKE '%$name%'";

note, that changing to 请注意,更改为

$query = 'SELECT username FROM members WHERE name LIKE "%$name%"';

will NOT do the trick. 不会成功

This is because single quoted strings do not interpret variables. 这是因为单引号字符串不能解释变量。

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. 注意:与双引号和heredoc语法不同,特殊字符的变量和转义序列在单引号引起来的字符串中不会扩展。

Double quoted strings however, will. 但是,会使用双引号引起来的字符串

The most important feature of double-quoted strings is the fact that variable names will be expanded. 双引号字符串的最重要特征是变量名称将被扩展。 See string parsing for details. 有关详细信息,请参见字符串解析。

UPDATE: This was heavily edited and changed in meaning, thanks to Memolition :) 更新:由于使用了Memolition,因此对其进行了大量编辑和含义更改:)

change 更改

$query = 'SELECT username FROM members WHERE name LIKE %$name%';

to

$query = "SELECT username FROM members WHERE name LIKE '%$name%'";

not

$query = 'SELECT username FROM members WHERE name LIKE "%$name%"';

because the double quotes as luksch says will print $name instead of $name variable value 因为luksch说的双引号将打印$ name而不是$ name变量值

so it has to be single quotes ' like this '%name' 因此它必须是单引号'这样的'%name'

so ' single quotes works for strings and variables 所以'单引号适用于stringsvariables

and " double quotes only works for strings "双引号仅适用于strings

You need to have single quotes around your the string you are using as the comparison for LIKE 您需要在用作LIKE比较的字符串周围加上单引号

In fact, since you have your query in single quotes what you are really querying against is: 实际上,由于您将查询用单引号引起来,因此真正要查询的是:

SELECT username FROM members WHERE name LIKE %$name%

with literal $name . 文字$name

Write your query like this: 这样编写查询:

$query = "SELECT username FROM members WHERE name LIKE '%$name%'";

You can also use Full Text index(only MyISAM AFAIR) and MATCH AGAINST clause. 您也可以使用全文本索引(仅MyISAM AFAIR)和MATCH AGAINST子句。 It will also give you rate of similarity. 它还将为您提供相似率。 Very useful in some cases. 在某些情况下非常有用。 Examples here: http://forum.kohanaframework.org/discussion/9182/mysql-full-text-searching-match-against/p1 此处的示例: http : //forum.kohanaframework.org/discussion/9182/mysql-full-text-searching-match-against/p1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM