[英]How can I write a query to select similar titles?
I would like to select those movies which have similar titles. 我想选择那些有类似头衔的电影。 I found this, but this way it dosn't work, it gives nothing. 我找到了这个,但这样它不起作用,它什么都没有。 I would like to give toy story 2, toy story 3 and others with similar title like toy soldielrs, etc. 我想提供玩具故事2,玩具故事3和其他类似标题的玩具销售商等。
$title = "Toy Story";
$query = mysql_query("SELECT title, year, poster, LEVENSHTEIN_RATIO( ".$title.", title ) as textDiff FROM movies HAVING textDiff > 60");
I can compare strings in PHP with this function: 我可以将PHP中的字符串与此函数进行比较:
static public function string_compare($str_a, $str_b)
{
$length = strlen($str_a);
$length_b = strlen($str_b);
$i = 0;
$segmentcount = 0;
$segmentsinfo = array();
$segment = '';
while ($i < $length)
{
$char = substr($str_a, $i, 1);
if (strpos($str_b, $char) !== FALSE)
{
$segment = $segment.$char;
if (strpos($str_b, $segment) !== FALSE)
{
$segmentpos_a = $i - strlen($segment) + 1;
$segmentpos_b = strpos($str_b, $segment);
$positiondiff = abs($segmentpos_a - $segmentpos_b);
$posfactor = ($length - $positiondiff) / $length_b;
$lengthfactor = strlen($segment)/$length;
$segmentsinfo[$segmentcount] = array( 'segment' => $segment, 'score' => ($posfactor * $lengthfactor));
}
else
{
$segment = '';
$i--;
$segmentcount++;
}
}
else
{
$segment = '';
$segmentcount++;
}
$i++;
}
// PHP 5.3 lambda in array_map
$totalscore = array_sum(array_map(function($v) { return $v['score']; }, $segmentsinfo));
return $totalscore;
}
But how can I compare in a SELECT query or any other way? 但是如何在SELECT查询或其他任何方式进行比较?
You can use like queries for that: Following example will return all the records from table customer for which customer name ends with kh 您可以使用类似的查询:以下示例将返回表customer的所有记录,其客户名称以kh结尾
select * from customer where name like '%kh'
Following example will return all the records from table customer for which customer name start with kh 以下示例将返回表customer的所有记录,其客户名称以kh开头
select * from customer where name like 'kh%'
Following example will return all the records from table customer for which the middle world of customer name is kh 以下示例将返回表客户的所有记录,客户名称的中间世界为kh
select * from customer where name like 'kh%'
if you want more specific record then add some and/or condition in your query 如果您想要更具体的记录,请在查询中添加一些和/或条件
I recommend you to read this http://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like 我建议你阅读http://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like
I think you might need to define how similar things need to be to be considered a match. 我想你可能需要定义类似的东西需要被认为是匹配。 But if you just wanna search for containing words, you could split your search string by whitespaces and use it in a REGEXP in your query 但是,如果您只想搜索包含单词,可以按空格分割搜索字符串,并在查询中的REGEXP中使用它
$search_array = explode(" ", "Toy story");
$query = "SELECT title, year, poster FROM movies WHERE title REGEXP '".implode("|", $search_array)."'";
This would probably match a lot rows, but you could make a more restrictive regular expression. 这可能会匹配很多行,但您可以制作更具限制性的正则表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.