0

Let's imagine abstract database, which contains objects with attached tags (default many-to-many relation with additional table). Now, we have a user, who wants to find objects matching the most of tags he provides, sorted by relevance. For example:

Object1: TagA, TagB, TagC, TagD
Object2: TagA,       TagC, TagD
Object3: TagB,             TagD, TagE, TagF
Object4: TagA, TagB, TagC,       TagE

User asks for TagB, TagC, TagD Expected result should look like following:

Object1 (3 matches)
Object4 (2 matches)
Object2 (2 matches)
Object3 (1 match)

Edit: Sample structure (I created it in SQLite, but we may safely assume MSSql)

CREATE TABLE Objects(Id int not null primary key, Name text);
CREATE TABLE Tags(Id int not null primary key, Name text);
CREATE TABLE ObjectTag(Id int not null primary key, ObjectId int, TagId int);

INSERT INTO Objects(Id, Name) VALUES (1, "Object1");
INSERT INTO Objects(Id, Name) VALUES (2, "Object2");
INSERT INTO Objects(Id, Name) VALUES (3, "Object3");
INSERT INTO Objects(Id, Name) VALUES (4, "Object4");

INSERT INTO Tags(Id, Name) VALUES (1, "TagA");
INSERT INTO Tags(Id, Name) VALUES (2, "TagB");
INSERT INTO Tags(Id, Name) VALUES (3, "TagC");
INSERT INTO Tags(Id, Name) VALUES (4, "TagD");
INSERT INTO Tags(Id, Name) VALUES (5, "TagE");
INSERT INTO Tags(Id, Name) VALUES (6, "TagF");

INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (1, 1, 1);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (2, 1, 2);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (3, 1, 3);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (4, 1, 4);

INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (5, 2, 1);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (6, 2, 3);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (7, 2, 4);

INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (8, 3, 2);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (9, 3, 4);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (10, 3, 5);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (11, 3, 6);

INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (12, 4, 1);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (13, 4, 2);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (14, 4, 3);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (15, 4, 5);

Expected result (for query "TagB, TagC, TagD"):

ObjectName | MatchCount
-----------+-----------
Object1    | 3
Object4    | 2
Object2    | 2
Object3    | 1
1
  • Can you please share your table(s)' structure? Also, what rdbms are you using?
    – Mureinik
    Commented Jan 31, 2016 at 9:10

2 Answers 2

0

Not sure about your table structure but from your post looks like you can use GROUP BY to achieve this like

select object, count(tag) as tagcount
from tags
where tag in ('TagB', 'TagC', 'TagD')
group by object;
0

Here's an answer:

SELECT t1.name, COUNT(t1.tag) as count FROM
    (SELECT o.Name as name, 
        1 as tag 
     FROM Objects AS o,
        ObjectTag AS ot 
     WHERE o.Id = ot.ObjectId 
         AND ot.TagId IN (2,3,4)) AS t1
     GROUP BY t1.name
     ORDER BY count DESC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.