tags:

views:

160

answers:

3

Given a collection of user specified tags how do I determine which ones are not in the tags table with 1 SQL Statement?

Assuming a table schema tags (id, tag) and I'm using mysql, if there's an optimization I'm unaware of.

thanks

+1  A: 
select * from canonical_list_of_tags where tag not in (select tag from used_tags)

At least that works in T-SQL for SQL Server ...

Edit: Assuming that the table canonical_list_of_tags is populated with the result of "Given a collection of user specified tags"

Unsliced
Cannonical_list_of_tags needs to be a pre-existing table for this to work...?Do I need to empty the table and then insert the user provided tags for this to work? Seems like it's be slower than just iterating over the tags individually? Am I wrong?
Allain Lalonde
Yes. Not sure what structures you have in MySQL, but SQL in the middle of loops should be converted into a single sql if possible. Consider a simple case, the table is so small it fits on one block. If you to LOOP/QUERY you'll read that same block over and over and over.
A: 
select
    utt.tagName
from
    userTypedTags utt
    left join tag t on utt.tagName = t.tag
where
    t.ID is null
    and utt.userID = <ID of the User in question>

That is assuming that you have table

userTypedTags(userID, tagName)

I have added a related question

kristof
+4  A: 

SELECT  Tag
FROM    UserSpecifiedTags
  LEFT OUTER JOIN AllTags ON UserSpecifiedTags.Tag = AllTags.Tag
WHERE   AllTags.Tag IS NULL

This should return what you want. In my experience, executing a join and looking for rows which don't have a match is much quicker than using the IN operator.

Chris Roberts