여러 LIKE 절을 구현하기 위한 구문은 다음과 같습니다. -
select * from yourTableName where yourColumnName1 LIKE ('%yourValue1%' or yourColumnName2 LIKE '%yourValue2%') or (yourColumnName3 LIKE '%yourValue3');
먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1534 -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(20), -> ClientAge int, -> ClientCountryName varchar(20) -> ); Query OK, 0 rows affected (0.78 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable1534(ClientName,ClientAge,ClientCountryName) values('Chris Brown',29,'AUS'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1534(ClientName,ClientAge,ClientCountryName) values('David Miller',49,'UK'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1534(ClientName,ClientAge,ClientCountryName) values('John Doe',43,'US'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1534(ClientName,ClientAge,ClientCountryName) values('Adam Smith',38,'US'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1534(ClientName,ClientAge,ClientCountryName) values('Carol Taylor',36,'UK'); Query OK, 1 row affected (0.16 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select * from DemoTable1534;
이것은 다음과 같은 출력을 생성합니다 -
+----------+--------------+-----------+-------------------+ | ClientId | ClientName | ClientAge | ClientCountryName | +----------+--------------+-----------+-------------------+ | 1 | Chris Brown | 29 | AUS | | 2 | David Miller | 49 | UK | | 3 | John Doe | 43 | US | | 4 | Adam Smith | 38 | US | | 5 | Carol Taylor | 36 | UK | +----------+--------------+-----------+-------------------+ 5 rows in set (0.00 sec)
다음은 단일 쿼리에서 여러 LIKE 연산자 사용에 대한 쿼리입니다 -
mysql> select * from DemoTable1534 -> where ClientName LIKE ('%Doe%' or ClientAge LIKE '%38%') or (ClientCountryName LIKE '%S');
이것은 다음과 같은 출력을 생성합니다 -
+----------+-------------+-----------+-------------------+ | ClientId | ClientName | ClientAge | ClientCountryName | +----------+-------------+-----------+-------------------+ | 1 | Chris Brown | 29 | AUS | | 3 | John Doe | 43 | US | | 4 | Adam Smith | 38 | US | +----------+-------------+-----------+-------------------+ 3 rows in set, 5 warnings (0.00 sec)