Im trying to implement small project where people can set tags for certain items and similar tags will be displayed on the page.
example:
Table : item_data
row 1:
item_id = 1 (event)
Tags = "Rugby, Tickets, Give away, Sesssions, Prizes"
row 2:
item_id = 2 (Ticket booking)
Tags = "New Zeland, Travel, Booking, Tickets, Rugby"
row 3:
item_id = 3 (car for sale)
Tags = "New Zeland, Blue, Ford, Sedan"
is there away purely in SQL to to get all matching item_ids for a given set of tags.
take the tags data from row 1 and find all the matching records for each tag in all the other rows?
I can do this with a mix of php and mysql, i was wondering if anyone have a better way to do this on sql alone.
I would recommend using Data Normalization (http://www.bkent.net/Doc/simple5.htm). if you are keeping tags in string formats like “tag a, tag b tag c, etc” then you have to select every tag in the DB break them in to arrays and do matching on that in order to find the relavant data.
I would recommend making a table for the Items and another for Tags that will grow as new tags are added then a third table mapping the item_id to the tag. Once this is done you can select based off specific tags and groups of tags to get results.
You would create an index in the map table and the tag table. and you would use this like so:
you would only have to change the last part in () to one tag or keep adding “or” and another tag, that will give you any and all item descriptions that match any given tag.
I know 3 tables looks like a lot to manage but trust me, if you item table and tag table begin to grow to the tens of thousands you (and you mysql server) will appreciate this style of data storage.