簡體   English   中英

mysql order by max匹配字段值

[英]mysql order by max matches field value

我共有三張桌子

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

所以結果應該是: test2,test3,test4

取決於標簽匹配和重量

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM