简体   繁体   English

mysql order by max匹配字段值

[英]mysql order by max matches field value

I have total three table like 我共有三张桌子

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 

here 这里

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

so i want closest tag match form test 1 所以我想要最接近的标签匹配表格测试1

so result should be: test2,test3,test4 所以结果应该是: test2,test3,test4

depend on tag match and weight 取决于标签匹配和重量

Try this: 尝试这个:

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 DEMO SQLFIDDLE演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM