Database/MYSQL

Mysql 초성 검색 function(mssql function수정)

타카스 류지 2016. 4. 15. 17:42

네비게이션 검색을 보면 초성 검색을 하는것을 보고 자료를 찾아보던 중 mssql 로 만든 펑션을 보고 mysql 버전으로 수정하였다.

 

검색시 like 로 검색을 하면은 참 용이하게 쓸거같다.

 

쿼리 : select chosung("메인보드(atx)")

 

결과 : ㅁㅇㅂㄷ(atx)

----------------------------------------------------------------------------

MYSQL

 

CREATE FUNCTION `chosung`(STR VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
 DECLARE I INT;
 DECLARE J INT;
 DECLARE TMPSTR VARCHAR(255);
 DECLARE COL1 VARCHAR(2);

 SET I = char_length(STR);
 SET J = 1;
 SET TMPSTR = '';
  WHILE J <=I  DO
    SET COL1 = SUBSTRING(STR, J, 1);
  SET TMPSTR = CONCAT(TMPSTR,(
                       CASE WHEN COL1 < 'ㄱ' THEN COL1 
                       WHEN ascii('ㄱ') <= ascii(COL1) and 
                              ascii(COL1)<= ascii('ㅎ') THEN COL1 
                       WHEN COL1 < '까' THEN 'ㄱ'
                       WHEN COL1 < '나' THEN 'ㄲ'
                       WHEN COL1 < '다' THEN 'ㄴ'
                       WHEN COL1 < '따' THEN 'ㄷ'
                       WHEN COL1 < '라' THEN 'ㄸ'
                       WHEN COL1 < '마' THEN 'ㄹ'
                       WHEN COL1 < '바' THEN 'ㅁ'
                       WHEN COL1 < '빠' THEN 'ㅂ'
                       WHEN COL1 < '사' THEN 'ㅃ'
                       WHEN COL1 < '싸' THEN 'ㅅ'
                       WHEN COL1 < '아' THEN 'ㅆ'
                       WHEN COL1 < '자' THEN 'ㅇ'
                       WHEN COL1 < '짜' THEN 'ㅈ'
                       WHEN COL1 < '차' THEN 'ㅉ'
                       WHEN COL1 < '카' THEN 'ㅊ'
                       WHEN COL1 < '타' THEN 'ㅋ'
                       WHEN COL1 < '파' THEN 'ㅌ'
                       WHEN COL1 < '하' THEN 'ㅍ'
                       WHEN COL1 <= '힣' THEN 'ㅎ'
                       ELSE COL1
                       END
         ));
      SET J = J + 1 ;
  END WHILE;
  RETURN TMPSTR;
END

 

----------------------------------------------------------------------------

 

MSSQL 원본

 

-- 한글인 경우, 자음 추출

-- 한글이 아닌 경우 그대로 출력

CREATE FUNCTION Parse_Str(@STR VARCHAR(255))

RETURNS VARCHAR(255)

AS

BEGIN

 

DECLARE @I INT, @J INT, @TMPSTR VARCHAR(255), @COL1 VARCHAR(2)

SET @I = LEN(@STR)

 

SET @J = 1

SET @TMPSTR = ''

 

        WHILE (@J<=@I)

        BEGIN

               SELECT @COL1 = SUBSTRING(@STR, @J, 1)               

       

               SET @TMPSTR = @TMPSTR +

                       CASE WHEN @COL1 < '' THEN @COL1

                       WHEN ascii('') <= ascii(@COL1) and

                              ascii(@COL1)<= ascii('') THEN @COL1

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 < '' THEN ''

                       WHEN @COL1 <= '' THEN ''

                       ELSE  @COL1

                       END

       

               SET @J = @J + 1

       

        END

RETURN(@TMPSTR)

END

GO

 

 

 

CREATE TABLE [good_table] (

  [good_cd]  [varchar] (13)  COLLATE Korean_Wansung_CI_AS NOT NULL ,

  [good_knm] [varchar] (100)  COLLATE Korean_Wansung_CI_AS NULL,

  [good_knm_sep] varchar(255) NULL -- 자음 추출한 데이터가 저장

 ) ON [PRIMARY]

GO

 

CREATE INDEX IDX__knm_sep ON good_table(good_knm_sep)

GO

 

CREATE TRIGGER TRG_GOOD_TABLE

ON GOOD_TABLE

FOR INSERT, UPDATE

AS

BEGIN

        UPDATE A

        SET good_knm_sep = dbo.Parse_Str(B.good_knm)

        FROM good_table A JOIN Inserted B ON A.good_cd = B.good_cd

END

GO

 

 

--)  '삼성파브' 입력시

INSERT GOOD_TABLE(good_cd, good_knm) VALUES ('12345','삼성파브')

GO

 

SELECT * FROM GOOD_TABLE

/*

good_cd         good_knm       good_knm_sep

-----------------------------------------

12345          삼성파브        ㅅㅅㅍㅂ

 

(1 적용됨)

*/