简体   繁体   中英

mySQL how to limit query returning all results containing a substring

lets consider an example. I want to search car and i'm getting all the results right.

But the problem is i also get all the results which contains car as a substring eg my result also return cartoon, care and every word that contains car as a substring in my database.

What i want is to apply a filter/condition so that it won't return words like cartoon and care, rather it should only return words like car and cars.

How can i achieve that? I have tried below solutions and i know what the problem is but i cannot understand how to solve it

$string.='(
            tbl_data.ad_title like "%'.$_REQUEST['searchtt'].'%"  
             or tbl_categories.cat_title like "%'.$_REQUEST['searchtt'].'%"           
          ) and ';

$gtdata = mysql_query(

"SELECT tbl_data.id, tbl_data.main_cat, tbl_data.sub_cat, 
        tbl_data.makevalue, tbl_data.ad_title, tbl_data.additional,
        tbl_data.city_name, tbl_data.city_area, 
        tbl_data.date1,tbl_data.date2,tbl_data.make_featured_active, 
        tbl_data.make_stoplight_active, tbl_data.make_urgent_active 
        FROM tbl_data 
        LEFT JOIN tbl_categories
        ON tbl_data.main_cat=tbl_categories.id 
        WHERE ".$string." tbl_data.status='1' and tbl_data.del=0
        and tbl_data.exp=0 and tbl_data.sold=0 and tbl_data.userblock='0'
       ".$orderby." 
       limit ".$limit_start.",".$limit_end."");
while($res_gtdata=mysql_fetch_array($gtdata))
{
//all results are stored in this variable
//$res
}

It sounds like you want to search for nouns in the singular and plural, for example "car" and "cars", "apple" and "apples". Is that correct?

If so, use a more specific where clause rather than like that just looks for singular and plural forms:

$singular = $_REQUEST['searchtt'];
$plural   = $singular.'s';

$gtdata = mysql_query(
"SELECT ...
FROM...
WHERE tbl_data.ad_title IN ('".$singular."', '".$plural."')
OR tbl_categories.cat_title IN ('".$singular."', '".$plural."')
...

This is a naive implementation that might work for you. Most plurals are easy - just add an 's' - but nouns ending in a vowel require 'es' , for example potato/potatoes, and there are lots of irregular forms, such as foot/feet and child/children. For details see How to search singular/plurals in php mysql

Another thing you need to know is whether the query ( searchtt ) is always singular. For example, do people always search for "car" rather than "cars"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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