February 5th, 2013, 03:48 AM
Get relationship id and use in same query
I wasn't sure if this was possible. I'm trying to avoid doing 2 querys. I have 3 simple tables
id, tag (string)
In my server side script I currently have the value for tags.tag and comments.id. What I need to do is insert into the tags_comments table the comments.id and corresponding tag.id that matches the tag string.
My question is there a way to retrieve the id value from the tags table and use it in the tags_comments insert based on the unique tag string. All in one query?
In my head the query would be,
Insert into tags_comments (comment_id, tag_id) values '2' , select tags.tag_id where tags_tag = %tagstring%
I hope this makes some sense.
February 5th, 2013, 04:09 AM
However, that "LIKE" stuff doesn't really fit your specification of a unique tag string. Why do you need it?
tags_comments (comment_id, tag_id)
tag LIKE '%...%'
February 5th, 2013, 04:43 AM
Thanks for that, after a little bit of playing I worked out what I was after. Sorry for the post.
Originally Posted by Jacques1
It was something like
INSERT INTO tags_comments VALUES(NULL, (SELECT tags.Id FROM tags WHERE tags.tag = "tag")