LAMP
MySQL find_in_set 함수 in_array 효과
영은파더♥
2016. 11. 2. 09:56
728x90
MySQL find_in_set 함수 in_array 효과
PHP 의 in_array 나 array_search 와 비슷한 기능을 하는 함수가 MySQL 에는 find_in_set 함수가 있다.
test_table
idx |
col1 | col2 |
1 |
1,2,3,4,5 | apple, banana, blueberry, cherry, graph |
2 |
2,3,4,5,6 | banana, blueberry, cherry, graph, lemon |
3 |
3,4,5,6,7 | blueberry, cherry, graph, lemon, mango |
4 |
4,5,6,7,8 | cherry, graph, lemon, mango, orange |
5 |
5,6,7,8,9 | graph, lemon, mango, orange, peach |
col1 컬럼에 7이 있는 데이터를 검색하는 쿼리
select * from test_table where find_in_set(7, col1)
결과 : idx 3,4,5 데이터
col2 컬럼에 banana 가 있는 데이터를 검색하는 쿼리
select * from test_table where find_in_set('banana', col2)
결과 : idx 1,2 데이터
이 함수가 없다면 정말 머리가 아플것이다.
find_in_set 함수가 있어서 유용하게 테이블을 설계할 수 있다.
만약 없다면 테이블 컬럼을 여러개 만들던가 테이블을 두개로 나누던가 아니면 프로그램에서 처리를 해야하는 케이스이다.
728x90
반응형