I have a table with column that contain comma separated ids like :
ids
----
1,2,3,4
1,4,5
1,5
2
2,6,9
Now I need to sort these ids based on a given string similarity (common elements). For example if the string be 1,5 the result I need is :
1,5 (exactly the same)
1,4,5 (has 1,5 but also has an extra number)
1,2,3,4 (has only 1)
2 (no matter)
2,6,9 (no matter)
My question is that MySQL has a built-in function to reach above result or I have to write a custom procedure? I tried match-against syntax but result was not acceptable.
Note:
In fact these numbers are tag id and I want to find the most similar product.
Thank you in advance
2
Answers
It is not the right approach to store comma-separate IDs, but I solved a similar problem in the past with this function:
It will score each match of the IDs(that worked for me, you might need to add some logic.
To find the count of matching numbers you can do:
output
see: DBFIDDLE
When putting more time in this you should be able to give
1,5
a higher ranking than1,4,5
.