I have a posts table structured this way:
id | title | content | tags
and a tags table
id | name | description
I put my tags in the posts table this way: 3,2,5,8 where the numbers are the tags table’s corresponding row number. Now what might me a good way to select related posts in my post by its tag(s)?
SELECT * FROM posts WHERE tags ? LIMIT 10
I’m quite bad at logic.
This is where database normalization comes in. Storing multiple tag ID’s in a single column is not a good idea. You should probably create a new table
post_tags, with structurepost_id | tag_idto link multiple tags to a single post.You can then execute the following query: