본문으로 바로가기

[MySQL] LIKE IN

category Mysql 4년 전
728x90
반응형

mysql 에도 LIKE IN 이 있는 줄 알고 조회 했더니  에러가 났다.

select * from news where title like in ('%test2%', '%test3%')

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'in ('%test2%', '%test3%')' at line 1

조사해 보니 mysql 에는 LIKE IN 이 없는 것 같다.

아래 참고사이트에 의하면  REGEXP 를 사용하면 된단다. 오홋!

사용할땐 LIKE IN 대신 REGEXP 를 넣고 문자열은 '검색하고싶은단어|검색하고싶은단어2' 구분자(|)를  넣어주면 된다.

select * from news where title  REGEXP  'test2|test3'

 

그리고 보통 IN 을 사용해서 아래와 같이 쓰는데 

select * from news where title  in (select name from products)

여기에 LIKE IN 처럼 사용하고 싶다면 group_concat 을 이용하면 된다.

select * from news where title  REGEXP (select group_concat(name separator'|') from products)

 

참고사이트

yamea-guide.tistory.com/entry/MySqlMariaDB-Like-in-%EA%B0%99%EC%9D%B4-%EC%93%B0%EA%B8%B0-%ED%95%B4%EA%B2%B0%EC%B1%85-REGEXP

728x90
반응형

Mysql카테고리의 다른글

[MySQL]DB 로그  (0) 2022.04.23
[MySQL]mysqldump where  (0) 2021.03.26
[MySQL]where 구에서 escape하는 방법  (0) 2021.01.30
[Mysql] Table size 조회  (0) 2020.10.29
[mysql]mysql 동작원리  (0) 2020.09.14