简体   繁体   中英

Mysql request () doesn't work after AND

when I have this request:

SELECT fr,text_id FROM texts 
WHERE (
    fr REGEXP '[>.:, ]le[<.:, ]' OR fr REGEXP '^le[[:space:]]?' 
    OR fr REGEXP '[[:space:]]le[[:space:]]?$'
  ) AND fr REGEXP '[>.:, ]la[<.:, ]' '' 
ORDER BY text_id DESC LIMIT 0, 20

It works

But when I put the () it doesn't work:

SELECT fr,text_id FROM texts 
WHERE (
    fr REGEXP '[>.:, ]le[<.:, ]' OR fr REGEXP '^le[[:space:]]?' 
    OR fr REGEXP '[[:space:]]le[[:space:]]?$'
  ) AND (fr REGEXP '[>.:, ]la[<.:, ]') '' 
ORDER BY text_id DESC LIMIT 0, 20

The point of putting the () is to put all the others OR together with the AND so the request shows only the result where all words typed are present in the text by searching for the ,. and other characters

So ideally this should work:

SELECT fr,text_id FROM texts 
WHERE (
    fr REGEXP '[>.:, ]le[<.:, ]' 
    OR fr REGEXP '^le[[:space:]]?' 
    OR fr REGEXP '[[:space:]]le[[:space:]]?$'
  ) AND (
    fr REGEXP '[>.:, ]la[<.:, ]' 
    OR fr REGEXP '^la[[:space:]]?'
  ) OR fr REGEXP '[[:space:]]la[[:space:]]?$'
  ) '' ORDER BY text_id DESC LIMIT 0, 20

If that may helps I use this loop to get the amount of words:

for($i=0;$i<$wordsLenght;$i++)
    {
        if($i !== 0)
        {   

        $recherche .= " )AND (".$lang." REGEXP '[>.:, ]".$words[$i]."[<.:, ]') ";
        }
        else
        {

                if($wordsLenght == 1)
                {
                    $recherche = "".$lang." REGEXP '[>.:, ]".$words[0]."[<.:, ]' OR (".$lang."     
                    REGEXP '^".$words[0]."[[:space:]]?') OR ".$lang." REGEXP  
                    '[[:space:]]".$words[0]."[[:space:]]?$'";                   
                }
                else
                {
                    $recherche = "(".$lang." REGEXP '[>.:, ]".$words[0]."[<.:, ]' OR ".$lang."    
                    REGEXP '^".$words[0]."[[:space:]]?' OR ".$lang." REGEXP 
                    '[[:space:]]".$words[0]."[[:space:]]?$'";
                }

        }
    }

It's in a function that is used as an argument in another fuction that part of it corresponds to the mysql request after WHERE.

Did you try to remove these seemingly superfluous '' after the last ) ?

SELECT fr,text_id FROM texts 
WHERE (
    fr REGEXP '[>.:, ]le[<.:, ]' OR fr REGEXP '^le[[:space:]]?' 
    OR fr REGEXP '[[:space:]]le[[:space:]]?$'
  ) AND (fr REGEXP '[>.:, ]la[<.:, ]')
ORDER BY text_id DESC LIMIT 0, 20

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