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

前端 未结 3 1876
感情败类
感情败类 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:51

    This seems to work:

    SET @j = '[3, 2, 1]';
    SELECT JSON_CONTAINS(@j, '3', '$');
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-17 16:06

    It is by design, although I can not agree with mySQL team. This should be implemented.

    https://bugs.mysql.com/bug.php?id=79233 [closed]

    The specification at https://dev.mysql.com/worklog/task/?id=7909 says: "This function returns path(s) to the given string. The returned path(s) identify object members or array slots which are character strings."

    So it seems that the intention of this function was to search for strings. The documentation should be updated to clarify that the function is for searching for string scalars, not for scalars in general.

    0 讨论(0)
提交回复
热议问题