#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    125
    Rep Power
    10

    Get relationship id and use in same query


    Hey guys,

    I wasn't sure if this was possible. I'm trying to avoid doing 2 querys. I have 3 simple tables

    comments
    id, comment

    tags
    id, tag (string)

    tags_comments
    comment_id, tag_id

    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.

    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    Code:
    INSERT INTO
    	tags_comments (comment_id, tag_id)
    SELECT
    	2, tag_id
    FROM
    	tags
    WHERE
    	tag LIKE '%...%'
    However, that "LIKE" stuff doesn't really fit your specification of a unique tag string. Why do you need it?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    125
    Rep Power
    10
    Originally Posted by Jacques1
    Hi,

    Code:
    INSERT INTO
    	tags_comments (comment_id, tag_id)
    SELECT
    	2, tag_id
    FROM
    	tags
    WHERE
    	tag LIKE '%...%'
    However, that "LIKE" stuff doesn't really fit your specification of a unique tag string. Why do you need it?
    Thanks for that, after a little bit of playing I worked out what I was after. Sorry for the post.

    It was something like

    INSERT INTO tags_comments VALUES(NULL, (SELECT tags.Id FROM tags WHERE tags.tag = "tag")

IMN logo majestic logo threadwatch logo seochat tools logo