카테고리 없음

[php] 단어 사이에 특수 문자를 추가하여 단어 검색

행복을전해요 2021. 2. 28. 12:32

If you know which characters to expect between the word-parts you can use regular expressions to query your database. For your examples this would work:

Find variants of XYZ2:

SELECT * FROM xytest WHERE val RLIKE 'XXX[- ]?2';

Find variants of YYYZZZ:

SELECT
  *
  FROM `xytest`
  WHERE val RLIKE 'YYY[- ]?ZZZ';
  

This uses MySQL's regular expression matching to allow an optional - or between word parts.

If there can be more characters in between you could change [- ] to e.g. [- _] (also recognizes _) or [^a-z0-9] (every non-letter non-number). The ? after the brace makes it's presence optional.

슬프게도 MySQL은 일치하는 항목을 알려줄 수 없지만 알아야 할 경우 PHP의 정규 표현식 엔진 중 하나에 동일한 정규 표현식을 제공 할 수 있습니다.

XXX2 또는 YYYZZZ 찾기 :

SELECT
  *
  FROM `xytest`
  WHERE val RLIKE 'XXX[- ]?2|YYY[- ]?ZZZ';
  

또는 물론

SELECT
  *
  FROM `xytest`
  WHERE val RLIKE 'XXX[- ]?2' OR val RLIKE 'YYY[- ]?ZZZ';
  
-------------------
SELECT * 
FROM  `table`
WHERE `column` LIKE  'XXX%'
OR `column` LIKE  'YYY%'
-------------------

1 개의 문자 변형으로 검색하려면 다음을 시도하십시오.

SELECT 
    * 
    FROM 
        mytable
        WHERE 
            mycol 
            LIKE 
                'XXX_YYY_ZZZ'
                
-------------------

LIKE 연산자를 사용하면

SELECT 
    * 
    FROM 
        mytable
        WHERE 
            mycol 
            LIKE 
                '%XXX%YYY%ZZZ%'
                


출처
https://stackoverflow.com/questions/22089778