2026, 새로운 도약을 시작합니다.

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

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

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

Copy


    $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에 

Copy


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

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

위의 쿼리문을 실행해보면

Copy


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개

채택된 답변
+20 포인트

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

Copy


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 = '홍길동'

로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

답변 감사합니다.
그런데 왜 반으로 나눠야 하는지는 아무리 생각해봐도 잘 모르겠는데...
이유를 혹시 알수 있을까요?
반이라고 했지만 정확히는 CHAR_LENGTH('||') 인거죠

댓글을 작성하려면 로그인이 필요합니다.

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

이 갯수에 맞춰

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

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

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

한번 고려해 보세요

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

답변감사합니다.
지금은.. sql이든 php든 javascript든.. 뭐든 다 어렵네요..

점점 더 어려워지는거 같습니다.

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고