您的位置:首页 > 数据库

一个文章有多个标签,根据这些标签找出相似度高的其他文章,SQL句子当时这么整,留做记录。

2018-01-25 10:20 302 查看
表: 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

虽然这个句子的执行效率不好,但最少拿出了我想要的数据了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql 相似度
相关文章推荐