There is no split function in mysql. You need to write it yourself: 1) obtain the number of strings separated by specified characters: SQL code DELIMITER $ DROPFUNCTIONIFEXISTS 'sims '. 'func _ get_split_string_total '$ CREATEDEFINER 'root' @ 'localhost' FUNCTION 'func _ get_split_string_total' (f_strin
There is no split FUNCTION in mysql. You need to write it yourself: 1) obtain the number of strings separated by specified characters: SQL code DELIMITER $ DROP FUNCTION IFEXISTS 'sims '. 'func _ get_split_string_total '$ create definer = 'root' @ 'localhost' FUNCTION 'func _ get_split_string_total' (f_strin
There is no split function in mysql and you need to write it yourself:
1) obtain the number of strings separated by specified characters:
SQL code
- DELIMITER $
-
- Drop function if exists 'sims'. 'func _ get_split_string_total '$
-
- Create definer = 'root' @ 'localhost' FUNCTION 'func _ get_split_string_total '(
- F_string varchar (1000), f_delimiter varchar (5)
- ) RETURNS int (11)
- BEGIN
- Declare returnInt int (11 );
- If length (f_delimiter) = 2 then
- Return 1 + (length (f_string)-length (replace (f_string, f_delimiter, '')/2;
- Else
- Return 1 + (length (f_string)-length (replace (f_string, f_delimiter ,'')));
- End if;
- END $
-
- DELIMITER;
For example, func_get_split_string_total ('abc | def | gh', '|') returns 3.
2) returns the string after the I-th segment.
SQL code
- DELIMITER $
-
- Drop function if exists 'sims'. 'func _ get_split_string '$
-
- Create definer = 'root' @ 'localhost' FUNCTION 'func _ get_split_string '(
- F_string varchar (1000), f_delimiter varchar (5), f_order int) RETURNS varchar (255) CHARSET utf8
- BEGIN
- Declare result varchar (255) default '';
- Set result = reverse (substring_index (f_string, f_delimiter, f_order), f_delimiter, 1 ));
- Return result;
- END $
-
- DELIMITER;
For example: func_get_split_string ('abc | def | gh', '|', 2) is def
3) Requirement: A field value in Table a is 1 | 2. In the select statement, A and B must be obtained through association with the B dictionary table.
SQL code
- Create definer = 'root' @ 'localhost' FUNCTION 'getdictname' (v_str varchar (100) RETURNS varchar (100) CHARSET utf8
- BEGIN
-
- DECLARE I int (4 );
- DECLARE dictCode varchar (100 );
- DECLARE dictName varchar (100 );
- DECLARE returnStr varchar (100 );
-
- Set I = 1;
- Set returnStr = '';
-
- If (v_str is null or length (v_str) = 0) then
- Return returnStr;
- Else
-
- While I <= func_get_split_string_total (v_str, '| ')
- Do
- Set dictCode = func_get_split_string (v_str, '|', I );
-
- Select names into dictName from sims_dd_dict where code = dictCode;
-
- Set returnStr = concat (returnStr, ',', dictName); -- use a comma (,) in Chinese. Otherwise, the data is serialized during EXCEL export because the program is separated by commas (,).
- Set I = I + 1;
- End while;
-
- Set returnStr = subString (returnStr, 2 );
- Return returnStr;
-
- End if;
- END $