MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 5th, 2013, 03:48 AM
doush. doush. is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 119 doush. User rank is Private First Class (20 - 50 Reputation Level)doush. User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 6 h 2 m 7 sec
Reputation Power: 8
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

Reply With Quote
  #2  
Old February 5th, 2013, 04:09 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 21 m 47 sec
Reputation Power: 813
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?

Reply With Quote
  #3  
Old February 5th, 2013, 04:43 AM
doush. doush. is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 119 doush. User rank is Private First Class (20 - 50 Reputation Level)doush. User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 6 h 2 m 7 sec
Reputation Power: 8
Quote:
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")

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsDatabasesMySQL Help > Get relationship id and use in same query

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap