Er.KT
Er.KT

Reputation: 2860

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

so result should be: test2,test3,test4

depend on tag match and weight

Upvotes: 1

Views: 103

Answers (1)

fortune
fortune

Reputation: 3372

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

Upvotes: 1

Related Questions