两个字符串用 ',' 分割,如下所示:
a:'1,2,3,4,5'
b:'6,2,1,3,9'
我想确定这些字符串是否有交集..
有什么功能可以在mysql中处理吗?
谢谢!
I must stress that just asking the question makes clear that your data structure is wrong. You should have a table for each of the pairs. It would have rows like:
a 1
a 2
. . .
b 6
b 2
etc.
And this question would be easily solved using standard SQL.
That said, if you are forced for some reason to do this, MySQL has some functions that can help. You can do something like:
where find_in_set(substring_index(substring_index(a, ',', 1), ',', -1), a) > 0 or
find_in_set(substring_index(substring_index(a, ',', 2), ',', -1), a) > 0 or
find_in_set(substring_index(substring_index(a, ',', 3), ',', -1), a) > 0 or
find_in_set(substring_index(substring_index(a, ',', 4), ',', -1), a) > 0 or
find_in_set(substring_index(substring_index(a, ',', 5), ',', -1), a) > 0 or
. . .
The expression substring_index(substring_index(a, ',', 5), ',', -1)
is a MySQL expression that returns the nth value in a string.