Skip to content

Instantly share code, notes, and snippets.

@vallerion
Last active December 23, 2019 17:23
Show Gist options
  • Select an option

  • Save vallerion/e7af20d1b28a1325f80925240bc2a93d to your computer and use it in GitHub Desktop.

Select an option

Save vallerion/e7af20d1b28a1325f80925240bc2a93d to your computer and use it in GitHub Desktop.
mysql equal json array of numbers
drop function sort_asc_json_array_numbers;
create function sort_asc_json_array_numbers (input_json_array text)
returns text
begin
declare result text default input_json_array;
set result = (
select json_arrayagg(val) from (
select val from JSON_TABLE(
input_json_array,
'$[*]' columns(
val int path '$'
)
) as rowsd
order by val
) as array_rows
);
return result;
end;
drop function equals_json_arrays;
create function equals_json_arrays (
first varchar(2048),
second varchar(2048)
)
returns bool
begin
declare i int default 0;
if JSON_LENGTH(first) != JSON_LENGTH(second) then
return false;
end if;
set first = sort_asc_json_array_numbers(first);
set second = sort_asc_json_array_numbers(second);
while i < JSON_LENGTH(first) DO
if JSON_EXTRACT(first, CONCAT('$[',i,']')) != JSON_EXTRACT(second, CONCAT('$[',i,']')) then
return false;
end if;
set i = i + 1;
END WHILE;
return true;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment