本文介绍了mysql order by max 匹配字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总共有三张桌子

table:Links

id | name | cat_id | weight
1 | test1 | 1 | 5
2 | test2 | 1 | 10
3 | test3 | 1 | 15
4 | test4 | 1 | 2

table:Tags

id | name | link_id
1 | tag1 | 1
2 | tag2 | 1
3 | tag1 | 2
4 | tag2 | 2
5 | tag1 | 3
6 | tag2 | 4

这里

test1 have tags like: tag1,tag2
test2 have tags like: tag1,tag2
test3 have tags like: tag1
test4 have tags like: tag2

所以我想要最接近的标签匹配表单测试 1

so i want closest tag match form test 1

所以结果应该是:测试2、测试3、测试4

取决于标签匹配和权重

推荐答案

试试这个:

SELECT DISTINCT(l.name) AS link
FROM links l INNER JOIN tags t ON l.id = t.link_id
AND l.id <> 1 GROUP BY t.link_id ORDER BY COUNT(t.link_id) DESC

SQLFIDDLE 演示

这篇关于mysql order by max 匹配字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 05:18