mySQL 구분자를 행으로 분리하기 질문드립니다.

mySQL 구분자를 행으로 분리하기 질문드립니다.

QA

mySQL 구분자를 행으로 분리하기 질문드립니다.

본문

구글링을 하다 저와 비슷하게 저장하는 분의 글을 보고 테스트를 해봤는데 

행은 잘 나눠지는데.. 원래 값보다 더 많이 나눠지는 현상이 발생했습니다.

 


    $sql = "
            select
               SUBSTRING_INDEX (SUBSTRING_INDEX(g5_write_test.wr_12, '||', numbers.n),'||',-1) value, wr_9
            from 
               (select  1 n union  all  select 2  
                union  all  select  3  union  all select 4 
                union  all  select  5  union  all  select  6
                union  all  select  7  union  all  select  8 
                union  all  select  9  union  all  select  10 
                union  all  select  11 union  all  select  12 
                union  all  select  13 union  all  select  14 
                union  all  select  15 union  all  select  16 
                union  all  select  17 union  all  select  18 
                union  all  select  19 union  all  select  20 ) numbers INNER  JOIN g5_write_test
                on CHAR_LENGTH ( g5_write_test.wr_12 ) 
                  - CHAR_LENGTH ( REPLACE ( g5_write_test.wr_12 ,  '||' ,  '' )) >= numbers.n-1
              and wr_29 = '2022-10-24' and wr_9 = '홍길동'
    ";
    $result = sql_query($sql, true);
    while ($row = sql_fetch_array($result)) {
        $list[] = $row;
    print_r2($row);
    }
    echo count($list);
 

 

wr_12에 


[wr_12] => 토마토||사과||사과||사과||오렌지||오렌지||오렌지||바나나||딸기

의 형식으로 || 구분자로 들어가 있습니다.

 

위의 쿼리문을 실행해보면


Array
(
    [value] => 토마토
    [wr_9] => 홍길동
)
Array
(
    [value] => 사과
    [wr_9] => 홍길동
)
Array
(
    [value] => 사과
    [wr_9] => 홍길동
)
Array
(
    [value] => 사과
    [wr_9] => 홍길동
)
Array
(
    [value] => 오렌지
    [wr_9] => 홍길동
)
Array
(
    [value] => 오렌지
    [wr_9] => 홍길동
)
Array
(
    [value] => 오렌지
    [wr_9] => 홍길동
)
Array
(
    [value] => 바나나
    [wr_9] => 홍길동
)
Array
(
    [value] => 딸기
    [wr_9] => 홍길동
)
Array
(
    [value] => 딸기
    [wr_9] => 홍길동
)
Array
(
    [value] => 딸기
    [wr_9] => 홍길동
)
Array
(
    [value] => 딸기
    [wr_9] => 홍길동
)
Array
(
    [value] => 딸기
    [wr_9] => 홍길동
)
Array
(
    [value] => 딸기
    [wr_9] => 홍길동
)
Array
(
    [value] => 딸기
    [wr_9] => 홍길동
)
Array
(
    [value] => 딸기
    [wr_9] => 홍길동
)
Array
(
    [value] => 딸기
    [wr_9] => 홍길동
)

 

총 17개의 값이 나옵니다.

마지막 딸기의 값이 계속 중복해서 나오는 현상이 발생합니다.

 

다른 게시글의 값을 불러와봐도 중구난방으로 마지막 값이 무작위로 복사가 되어서 나오고 있습니다.

 

구분자의 개수가 정해져 있으면 상관이 없는데.. 1개에서부터 3-40개정도까지 늘어날수 있는 부분이라

딱 정해서 개수를 지정할 수는 없습니다.

 

정상적인 값이 출력될려면 어떻게 수정해야 할까요?

이 질문에 댓글 쓰기 :

답변 2

조인절에서 가상번호와 비교하는 구분자 개수차이 부분을 반으로 나눠줘야 맞는게 아닌가 싶습니다.


select
   SUBSTRING_INDEX (SUBSTRING_INDEX(g5_write_test.wr_12, '||', numbers.n),'||',-1) value, wr_9
from 
   (select  1 n union  all  select 2  
    union  all  select  3  union  all select 4 
    union  all  select  5  union  all  select  6
    union  all  select  7  union  all  select  8 
    union  all  select  9  union  all  select  10 
    union  all  select  11 union  all  select  12 
    union  all  select  13 union  all  select  14 
    union  all  select  15 union  all  select  16 
    union  all  select  17 union  all  select  18 
    union  all  select  19 union  all  select  20 ) numbers INNER  JOIN g5_write_test
    /*
    on CHAR_LENGTH ( g5_write_test.wr_12 ) 
      - CHAR_LENGTH ( REPLACE ( g5_write_test.wr_12 ,  '||' ,  '' )) >= numbers.n-1
    */
    ON ((CHAR_LENGTH ( g5_write_test.wr_12 ) - CHAR_LENGTH ( REPLACE ( g5_write_test.wr_12 ,  '||' ,  '' ))) / 2) >= numbers.n-1
  and wr_29 = '2022-10-24' and wr_9 = '홍길동'

[wr_12] => 토마토||사과||사과||사과||오렌지||오렌지||오렌지||바나나||딸기

이 갯수에 맞춰

Join 갯수가 있어야 하지 않을까요.

어려운 sql 말고 PHP로 처리하는 것이 좋을 듯합니다. 아니면

별도 테이블로 저장하는 것도

한번 고려해 보세요

 

답변을 작성하시기 전에 로그인 해주세요.
전체 167
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT