'Check if two json arrays are equal regardless of the order in MySQL

I have a MySQL table:

CREATE TABLE `test_table` (
    `id` BIGINT,
    `json_config` JSON,
);

json_config has a property called bookNames, whose value is a json array:

{ "bookNames": ["math", "history"] }

I want to check if any json_config's bookNames equals to ["history", "geography"] regardless of the order. Something like this:

select * from table_name
where sort(json_config.bookNames) = sort('["history", "geography"]')

The result should contain records whose bookNames is ["history", "geography"] or ["geography", "history"], because it doesn't care about the order. How can I do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source