MYSQL Triggers: JSON_SEARCH an integer value in a json array of integers

前端 未结 3 1893
感情败类
感情败类 2020-12-17 15:23

I am looking to use json_search to get the array path that corresponds to a value.

I have tried and this works:

SET @j = \'[\"3\", \"2\", \"1\"]\         


        
3条回答
  •  孤街浪徒
    2020-12-17 15:56

    I have tried a workaround method, which is to CONCAT the string and cast it back to JSON and update the table. It's not the most elegant way, so if you guys have any suggestion, please let me know! :)

    BEGIN
    DECLARE var1 INT default -2; //offsets
    DECLARE var2 INT default 2; //offsets
    SELECT StatusID into @statusList FROM UserStatusesIndex;
    SET @statusID_to_remove = OLD.StatusID;
    SET @startIndex = INSTR(@statusList, @statusID_to_remove);
    SET @charLength = CHAR_LENGTH(@statusID_to_remove);
    
    IF @startIndex <= 2 THEN SET var1=-1, var2=2; //If its the first index
    ELSEIF (CHAR_LENGTH(@statusList) - @startIndex <= 2) THEN SET var1=-3, var2=0;  //If its the last index
    ELSE SET var1=-2, var2=2;
    END IF;
    
    SET @newJson=CAST(CONCAT(SUBSTRING(@statusList, 1, @startIndex+var1), SUBSTR(@statusList, @startIndex + @charLength+var2, CHAR_LENGTH(@statusList) - @startIndex - @charLength+2)) as JSON);
    UPDATE UserStatusesIndex SET StatusID=@newJson WHERE StatusCreator=OLD.StatusCreator;
    END
    

    Thanks!

提交回复
热议问题