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

MySQL 그룹화된 레코드에 일치하는 문자열이 여러 개 있는 경우 선택합니까?

<시간/>

이를 위해 정규식을 사용할 수 있습니다. 먼저 테이블을 생성하겠습니다 -

mysql> create table DemoTable
   (
   ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ProductName varchar(20)
   );
Query OK, 0 rows affected (0.19 sec)

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

mysql> insert into DemoTable(ProductName) values('Product-1');
Query OK, 1 row affected (0.05 sec)

mysql> insert into DemoTable(ProductName) values('Product2');
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable(ProductName) values('Product1');
Query OK, 1 row affected (0.04 sec)

mysql> insert into DemoTable(ProductName) values('Product-3');
Query OK, 1 row affected (0.05 sec)

mysql> insert into DemoTable(ProductName) values('Product3');
Query OK, 1 row affected (0.05 sec)

mysql> insert into DemoTable(ProductName) values('Product-4');
Query OK, 1 row affected (0.09 sec)

mysql> insert into DemoTable(ProductName) values('Product4');
Query OK, 1 row affected (0.05 sec)

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

mysql> select *from DemoTable;

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

+-----------+-------------+
| ProductId | ProductName |
+-----------+-------------+
| 1         | Product-1   |
| 2         | Product2    |
| 3         | Product1    |
| 4         | Product-3   |
| 5         | Product3    |
| 6         | Product-4   |
| 7         | Product4    |
+-----------+-------------+
7 rows in set (0.00 sec)

다음은 일치하는 문자열이 여러 개 있는 그룹화된 레코드에 대한 쿼리입니다. -

mysql> select *from DemoTable
where ProductName regexp 'Product[1234].*';

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

+-----------+-------------+
| ProductId | ProductName |
+-----------+-------------+
| 2         | Product2    |
| 3         | Product1    |
| 5         | Product3    |
| 7         | Product4    |
+-----------+-------------+
4 rows in set (0.00 sec)