Computer >> 컴퓨터 >  >> 프로그램 작성 >> MySQL

문자열에서 정확한 단어를 검색하는 MySQL 쿼리?

<시간/>

문자열에서 정확한 단어를 검색하려면 아래 구문을 사용하십시오 -

select *from yourTableName
where
yourColumnName regexp '(^|[[:space:]])yourWord([[:space:]]|$)';

먼저 테이블을 생성하겠습니다 -

mysql> create table DemoTable
   (
   Title text
   );
Query OK, 0 rows affected (0.23 sec)

삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -

mysql> insert into DemoTable values('This is the Introduction to Java');
Query OK, 1 row affected (0.05 sec)

mysql> insert into DemoTable values('This is the Introduction to MongoDB');
Query OK, 1 row affected (0.07 sec)

mysql> insert into DemoTable values('This is the Introduction to MySQL');
Query OK, 1 row affected (0.06 sec)

select 문을 사용하여 테이블의 모든 레코드 표시 -

mysql> select *from DemoTable;

이것은 다음과 같은 출력을 생성합니다 -

+-------------------------------------+
| Title                               |
+-------------------------------------+
| This is the Introduction to Java    |
| This is the Introduction to MongoDB |
| This is the Introduction to MySQL   |
+-------------------------------------+
3 rows in set (0.00 sec)

다음은 문자열에서 정확한 단어를 검색하는 쿼리입니다.

mysql> select *from DemoTable
where
Title regexp '(^|[[:space:]])MySQL([[:space:]]|$)';

이것은 다음과 같은 출력을 생성합니다 -

+-----------------------------------+
| Title                             |
+-----------------------------------+
| This is the Introduction to MySQL |
+-----------------------------------+
1 row in set (0.13 sec)