表: table
id title content labels
1 aa bbb-cc 1,2,3,4,5
2 bb aa-c-d 2,4,5,6,7
3 cc xxxxxx 2,3,4,5,6
4 dd yyyyyy 4,5,6,7,8
假如现在显示的是ID为1的那条,那么 需要找出 labels 里面相似度最高的几条数据,当时的句子如下:
SELECT
id,
((if(find_in_set('1',`labels`),1,0)) +
(if(find_in_set('2',`labels`),1,0)) +
(if(find_in_set('3',`labels`),1,0)) +
(if(find_in_set('4',`labels`),1,0)) +
(if(find_in_set('5',`labels`),1,0))) as e,
labels
FROM
`table`
WHERE
find_in_set('1',`labels`) OR
find_in_set('2',`labels`) OR
find_in_set('3',`labels`) OR
find_in_set('4',`labels`) OR
find_in_set('5',`labels`)
ORDER BY
e Desc
LIMIT 10
虽然这个句子的执行效率不好,但最少拿出了我想要的数据了。