Can you split/explode a field in a MySQL query?

后端 未结 17 1081
无人及你
无人及你 2020-11-22 04:03

I have to create a report on some student completions. The students each belong to one client. Here are the tables (simplified for this question).

CREATE TAB         


        
17条回答
  •  春和景丽
    2020-11-22 04:45

    Well, nothing I used worked, so I decided creating a real simple split function, hope it helps:

        DECLARE inipos INTEGER;
        DECLARE endpos INTEGER;
        DECLARE maxlen INTEGER;
        DECLARE item VARCHAR(100);
        DECLARE delim VARCHAR(1);
    
        SET delim = '|';
        SET inipos = 1;
        SET fullstr = CONCAT(fullstr, delim);
        SET maxlen = LENGTH(fullstr);
    
        REPEAT
            SET endpos = LOCATE(delim, fullstr, inipos);
            SET item =  SUBSTR(fullstr, inipos, endpos - inipos);
    
            IF item <> '' AND item IS NOT NULL THEN           
                USE_THE_ITEM_STRING;
            END IF;
            SET inipos = endpos + 1;
        UNTIL inipos >= maxlen END REPEAT;
    

提交回复
热议问题