Best Way to make a "hashtag" system Best Way to make a "hashtag" system php php

Best Way to make a "hashtag" system


Three tables will do the trick:

Contents, Hashtags, and ConTags. ConTags will be a junction table containing contents.id and hashtags.id. In this way, you can attribute multiple hashtags to each item in Contents.

SELECT * FROM Contents c, Hashtags h LEFT JOIN ConTags t ON c.Id = t.ConId AND h.Id = t.HashId

Alternatively, set Hashtags Name and ContentId as unique key and the junction table is no longer needed


Actually, one extra table is enough

"hashtags" (id(INT11), hashtag(VARCHAR(40))), content_id(int11))

Now you can simply add hastag's by name. To get all hashtags for a content, use

SELECT hashtag FROM hashtable WHERE content_id=$content_id

TO add a remove a hashtag, delete it using its id or hashtag itself. To get content for specific hastags, just use

SELECT ct.* from hashtable ht, contenttable ct WHERE ht.hashtag=$hastag and ct.id=ht.content_id

and so on