views:

61

answers:

1

Im working on an image sharing site and want to implement tagging for the images.

I've read Questions #20856 and #2504150

I have few concerns with the approach on the questions above. First of all it looks easy to link an image to a tag. However getting images by tag relation is not as easy. Not easy because you will have to get the image-to-tag relation from one table and then make a big query with a bunch of OR statements( one OR for every image).

Before i even research the tagging topic i started testing the following method:

This tables as examples:

Table: Image
Columns: ItemID, Title, Tags

Table: Tag
Columns: TagID, Name

The Tags column in the Image table takes a string with multiple tagID from the Tag table enclosed by dashes(-).

For example:

-65-25-105- 

Links an image with the TagID 65,25 and 105.

With this method i find it easier to get images by tag as i can get the TagID with one query and get all the images with another simple query like:

SELECT * FROM Image WHERE Tags LIKE %-65-%

So if i use this method for tagging,

How effective this is?

Is querying by LIKE %-65-% a slow process?

What problems can i face in the future?

+2  A: 

You need 3 tables for this.

Table: Image
Columns: ImageId, ItemID, Title

Table: Image_Tag
Columns: ImageId, TagId

Table: Tag
Columns: TagID, Name

Then to get all images for a tag you would use:

SELECT ImageId, Title 
FROM Image_Tag LEFT JOIN Image USING (ImageId)
WHERE TagId = $TagId

This is the typical way to handle many-to-many relationships in a relational database. You would probably benefit by reading about http://en.wikipedia.org/wiki/Database_normalization

Edit: I see this was already addresses in the other questions you referenced, so I'll explain further. The biggest problem I see of doing it your way is you can't take advantage of indexing your id columns which makes your queries less efficient. It also looks like it would be clumsy to update. I would highly suggest not trying to do it that way, at least try out using the 3 table solution. Once it "clicks" for you you'll thank me.

Syntax Error
Thanks for your input, is just that i've read plenty of stories of LEFT JOIN being slow. I'll go with the three tables, people seem to really dig this.
Pablo