I have two tables:
posts:
id | ... other stuff ... | tags ----+---------------------+-------------- 1 | ... | <foo><bar> 2 | ... | <foo><baz><blah> 3 | ... | <bar><blah><goo>
and tags:
tag -------------- <foo> <bar> <baz> <blah> <goo>
posts.tags and tags.tag are both types of text. What I want is the relation from tags.tag to the lines in the messages, so the <foo> request will give me the lines corresponding to messages 1 and 2, the <blah> request gives me 2 and 3, <bar> gives me 1 and 3, etc.
I looked at foreign keys, but I'm not sure what I want. (and to be honest, I'm not quite sure what he is doing). From what I can tell, the foreign key should be equal to the primary key / unique column of the table. But I want all the lines such that posts.tags ~ '.*<foo>.*' Etc. I also want to be able to, say, get all the tags starting with b, for example:
CREATE VIEW startswithB AS SELECT tag FROM tags WHERE tag ~ '<b.*>'; SELECT DISTINCT * FROM posts, startswithB WHERE posts.tags ~ ('.*' || startswithB || '.*');
How do I get the attitude I'm looking for? Is it possible?
EDIT:
Good thing I did:
Create post_tags:
SELECT posts.id, tags.tag INTO post_tags FROM posts, tags WHERE posts.tags ~ ('.*' || tags.tag || '.*');
select all posts tagged with <foo> :
SELECT * FROM posts WHERE posts.id IN ( SELECT id FROM post_tags WHERE tag = '<foo>' );
Eric W.
source share