R
R
rundll322020-08-10 14:13:51
PHP
rundll32, 2020-08-10 14:13:51

Tag search algorithm?

If the database table has a tags string field that contains tags separated by commas, how best to implement search results? Let's say the first result is an entry with a match of 3\3, then 2\3 and 1\3.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Tikhonov, 2020-08-10
@rundll32

SELECT record.id, count(*) 
FROM record 
   JOIN record_tags ON (record_tags.record_id = record.id)
WHERE record_tags.tag_id IN (1,2,3) GROUP BY 1 ORDER BY 2 DESC

In a many-to-many data structure, get all records associated with the tags you are looking for, and sorting will return the number of matches (one record with different tags)

T
ThunderCat, 2020-08-10
@ThunderCat

No way. You cannot store tags in a text field with tags written in a row. To do this, they make a many-to-many relationship, where the tags are in a separate table, the records are separate and there is a table of tag links to records.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question