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

MySQL에서 부분적으로 일치하는 값을 선택하는 방법이 있습니까?

<시간/>

부분적으로 일치시키려면 LIKE 연산자를 사용하십시오. 먼저 테이블을 생성하겠습니다 -

mysql> create table DemoTable806(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(100),
   StudentSubject varchar(100)
);
Query OK, 0 rows affected (0.57 sec)

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

mysql> insert into DemoTable806(StudentName,StudentSubject) values('Chris','Java in Depth With Data Structure');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable806(StudentName,StudentSubject) values('Robert','Introduction to MySQL');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable806(StudentName,StudentSubject) values('Bob','C++ in Depth With Data Structure And Algorithm');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable806(StudentName,StudentSubject) values('Adam','Introduction to MongoDB');
Query OK, 1 row affected (0.11 sec)

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

mysql> select *from DemoTable806;

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

+-----------+-------------+------------------------------------------------+
| StudentId | StudentName | StudentSubject                                 |
+-----------+-------------+------------------------------------------------+
|         1 | Chris       | Java in Depth With Data Structure              |
|         2 | Robert      | Introduction to MySQL                          |
|         3 | Bob         | C++ in Depth With Data Structure And Algorithm |
|         4 | Adam        | Introduction to MongoDB                        |
+-----------+-------------+------------------------------------------------+
4 rows in set (0.00 sec)

다음은 부분적으로 일치하는 값을 선택하는 쿼리입니다 -

mysql> select *from DemoTable806 where StudentSubject LIKE '%Depth%';

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

+-----------+-------------+------------------------------------------------+
| StudentId | StudentName | StudentSubject                                 | 
+-----------+-------------+------------------------------------------------+
|         1 | Chris       | Java in Depth With Data Structure              |
|         3 | Bob         | C++ in Depth With Data Structure And Algorithm |
+-----------+-------------+------------------------------------------------+
2 rows in set (0.00 sec)