Mysql搜索字符串

时间:2011-12-16 04:28:44

标签: php mysql sql

我有一个搜索框,会在字段中搜索关键字:description 然后返回结果。它在mysql和php中完成。 描述的长度可以是10到600个字符。我想只返回一部分结果。 对于示例:  我正在搜索关键字=约翰 而不是显示整个字段 description作为结果我想看到仅找到关键字的句子:...你好我的名字是约翰和我...... 不是整段。

我可以使用sql或php,无论哪个更好。我试过子串但是没有实现它,任何一个例子都会很棒。

SELECT  SUBSTR(description,2,100)  
FROM screenplays 
WHERE lower(description) like '% $search_word_fix %'

4 个答案:

答案 0 :(得分:2)

我认为通过查询,它会妨碍性能。因为字符串计算。所以我通过php substr函数逻辑完成了这个。

SELECT  description 
        FROM screenplays 
        WHERE lower(description) like '% $search_word_fix %'

//$description will have longer string.
//$short_string will have shorter string.

    $short_string=get_less_string($description,'john',20);

    function get_less_string($string,$key,$num){
             $desc=explode($key,$string);
             $left='...'.substr($desc[0], strlen($desc[0])-$num,$num);
             $right=substr($desc[1],0,$num).'...';
             $final_string=$left.$key.$right;
             return $final_string;
        }

答案 1 :(得分:1)

如果你想使用php ..几个月前我写了一个函数。它在文本中搜索关键字并返回带有“粗体”关键字的字符串。希望这会有所帮助。

    $str = 'I have a search box that will search for a keyword in the field : description then return results. its done in mysql and php. the description can go from 10 to 600 characters long. I would like to return only a part of result. for example : I am searching for the keyword= John instead of displaying the whole field description as the result I want to see the sentence where the keyword is found only: ... Hello my name is john and I ... not the whole paragraph.

    I can use sql or php whichever one is better. I tried substring but failed implementing it any example would be great.';
    $search = array();
    $search[] = "example";
    echo descriptionSearch($str,$search);
    function descriptionSearch($desc,$words,$max_number_words = 30)
    {
                $descs = explode(" ",$desc);
                $positions = array();
                $i = 0;
                foreach($words as $w)
                {
                    $j = 0;
                    foreach($descs as $d)
                    {
                        $w = strtolower(trim($w));
                        $d = strtolower(trim($d));
                        if(strpos($d,$w) !== false)
                        {
                            $positions[] = $j;
                            $descs[$j] = str_replace($w,"<span style='font-weight: bold;'>".$d."</span>",$d);
                        }
                $j++;
            }
            $i++;
        }
        $max = 0;
        if(sizeof($positions) > 0)
        {
            foreach($positions as $j)
            {
                if($max < 4)
                {
                    $i = $j -5;
                    if($i < 0)
                    {
                        $i = 0;
                    }
                    while($i < ($j+10))
                    {
                        $toreturn .= $descs[$i]." ";
                        $i++;
                    }
                    $toreturn .=  " ...";
                    $max++;
                }
            }
        }
        else
        {
            for($i=0; $i < $max_number_words; $i++)
            {
                $toreturn .=  $descs[$i]." ";
            }
        }
        return $toreturn;
    }

答案 2 :(得分:1)

你可以这样做,两者都会有效率。 PHP可能是一个更灵活的解决方案,但只是为了踢,我试图在MySQL中做,看看它是如何工作的

SELECT SUBSTR(
          description,
          if(INSTR(description, '$search_word_fix') - 10 < 0,
             0,
             (INSTR(description, '$search_word_fix') - 10)),
          if(
             INSTR(description, '$search_word_fix') + 10 >
                length(description),
             length(description),
             NSTR(description, '$search_word_fix') + 10))
  FROM screenplays
 WHERE lower(description) LIKE '% $search_word_fix %'

答案 3 :(得分:1)

修改后的答案,这可以更好地搜索。在这个例子中。我正在寻找&#39;例如&#39;或者&#39; john&#39;

        $str = 'I have a search box that will search for a keyword in the field : description then return results. its done in mysql and php. the description can go from 10 to 600 characters long. I would like to return only a part of result. for example : I am searching for the keyword= John instead of displaying the whole field description as the result I want to see the sentence where the keyword is found only: ... Hello my name is john and I ... not the whole paragraph.

            I can use sql or php whichever one is better. I tried substring but failed implementing it any example would be great.';
        $search = array();
        $search[] = "for example";
        $search[] = "john";
        echo descriptionSearch($str,$search);
        function descriptionSearch($desc,$words,$max_number_words = 30)
        {
            $positions = array();
            $i = 0;
            foreach($words as $w)
            {
                    $w = strtolower(trim($w));
                    $d = strtolower(trim($d));
                    if(strpos($desc,$w) !== false)
                    {
                        $positions[] = strpos($desc,$w);
                        $desc = str_replace($w,"<span style='font-weight: bold;'>".substr($desc,strpos($desc,$w),strlen($w))."</span>",$desc);
                    }
                $i++;
            }
            $max = 0;
            if(sizeof($positions) > 0)
            {
                foreach($positions as $j)
                {
                    if($max < 4)
                    {
                        $i = $j -16;
                        if($i < 0)
                        {
                            $i = 0;
                        }
                            $toreturn .= substr($desc,$i,80);
                        $toreturn .=  " ...";
                        $max++;
                    }
                }
            }
            else
            {
            $descs = explode(" ",$desc);
                for($i=0; $i < $max_number_words; $i++)
                {
                    $toreturn .=  $descs[$i]." ";
                }
            }
            return $toreturn;
        }