The JSON data is as follows:
I would like to count how many times each element in a collection, such as {"3467562849402896", "3467562861985809", "3465044211793921"}, has been used. The number of elements in the collection is not fixed and is an input parameter.
I am aware that JSON_TABLE in MySQL 8 can be used, but my version is 5.7.
One approach I thought of is to first use the following SQL to find out who used this ID, and then handle the frequency count in the code.
where JSON_CONTAINS(tags ,'"3467562849402896"' , '$')
OR JSON_CONTAINS(tags, '"3467562861985809"', '$')
OR JSON_CONTAINS(tags, '"3465044211793921"', '$');
Is there a better solution to this issue?
2
Answers
Schematically:
if you need only in those rows which contains at least one matched value then use INNER JOIN and COUNT(*).
Here is a way you can do that easily.
convert tags to rows
count entrys
sample
create table
insert data
run first query – tags to rows
run second query – count duplicates
sample : https://dbfiddle.uk/o5Pj2J0i
change the line singletag int(64) path ‘$’)) as j to your datatype ie. VARCHAR
Note: the Sample is for MySQL Version 8