views:

44

answers:

1

Hi,

I'm working on an PHP/Jquery autosuggest tool that'll query a large table of tags but I need some suggestions for the db schema that'll return related tags as well as match-based results.

For example:

I type 'Web':

Results

'Web Developer'

'Web Designer'

'Web Analyst'

and also related tags...

'PHP Developer'

'Flash Programmer'

So obviously there needs to be a column for each tag with some sort of relational or parentage value... Whats the best way to go about this?

Thanks!

--- UPDATE -----------------

certainly - and thanks again for all your help!

here are the tables..

Tags

Id TagName

1 Web Developer

2 Web Designer

3 Web Analyst

4 PHP Developer

5 Flash Programmer

RelatedTags

TagId RelatedTagId Strength

1 4 0

1 5 7

Since the Strength value of 'Flash Programmer' is 7 i'd like it to appear before PHP programmer..(hopefully the format is clear enough..)

thanks again!

+1  A: 

Table Tags

Create Table Tags (Id int, TagName varchar(50))

Table RelatedTags (both fields FK to Tags(Id) )

Create Table RelatedTags(TagId int, RelatedTagId int)

Query for selecting tag matched input and related tags

SELECT Id, TagName, Strength
FROM Tags
WHERE TagName LIKE 'Web%'
UNION
SELECT Id, TagName, Strength
FROM Tags 
WHERE tags.Id IN (SELECT RelatedTagId
       FROM Tags t
       JOIN RelatedTags r
       ON (t.Id = r.TagId)
       WHERE t.tagName LIKE 'Web%')
ORDER By 3 DESC
Michael Pakhantsov
that looks great... though i'm getting a syntax error (near 'SELECT RelatedTagId FROM Tags t JOIN RelatedTags r ON (t.Id' at line 7) 0 - i cant see it ;(
Fearghal
@Fearghal, Yup, was a typo, I corrected query
Michael Pakhantsov
wait nevermind... got it... excellent - looks brill - thanks!
Fearghal
so i've added a 'Strength' column (SMALLINT) to the related table... I'm going to use this specify the ordering of results based on the closeness of the match - for example I want Flash to appear before PHP... I give Flash a strength of 7 and PHP 4 - so where do i put the ordering clause?
Fearghal
tried this but know it wasnt right - SELECT Id, TagNameFROM TagsWHERE TagName LIKE 'Web%'UNIONSELECT Id, TagNameFROM Tags WHERE Tags.Id IN (SELECT RelatedTagId FROM Tags t JOIN RelatedTags r ON (t.Id = r.TagId) WHERE t.tagName LIKE 'Web%') ORDER BY r.Strength DESC
Fearghal
@Fearghal, added Strength field into query
Michael Pakhantsov
Hi Michael... sorry - the strength column is in the RelatedTags table which the above sql can't find... is it a matter of simply adding the relatedTags table to the initial SELECT statement? thanks for your help
Fearghal
@Fearghal, can you explain in details how you want sort results?
Michael Pakhantsov
sure... i'll put it in a current post for formatting purposes
Fearghal
i've updated the post - (under --- UPDATE -----------------)
Fearghal