Query that ignore the spaces

后端 未结 6 1530
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 02:59

What\'s the best way to run a query so that spaces in the fields are ignored? For example, the following queries:

SELECT * FROM mytable WHERE username = \"Jo         


        
6条回答
  •  迷失自我
    2020-11-30 03:43

    We often want to search for text, regardless of the number of spaces, white space and letters.

    Just trim , lower case , and replace all multipe Non-word characters for one space.

    SELECT regexp_replace(trim(lower('Here is              a            long               text               , with            many                 white spaces         AND             different                 character              sensitive')),'\W+',' ','g') t 
    

    return :here is a long text with many white spaces and different character sensitive

    Here is the use for search. Only the order of words is important, nothing more.And this is beautiful.

    select * from (
    SELECT regexp_replace(trim(lower('Here is              a            long               text               , with            many                 white spaces         AND             different                 character              sensitive')),'\W+',' ','g') t 
    ) as o
    where t= regexp_replace(trim(lower('Here  is a LonG      TEXT , with            mANY white   ^    spaces         AND           different  character              sensiTive')),'\W+',' ','g')
    

    return:here is a long text with many white spaces and different character sensitive

    Garbage in data and junk in the query, but it still finds it right.

提交回复
热议问题