?
Solved

MySQL Insert into table from two tables

Posted on 2011-09-16
8
Medium Priority
?
451 Views
Last Modified: 2012-05-12
I need help inserting ContactID  from a contacts (table) into a lead_tracking (table) with the same field name of ContactID.

I need to use some sort of conditional logic to parse out the records that have a ContactID with the ContactStatusID = 2 in the contacts table. With that information I need to have two preset values for two addtional fields in the lead_tracking table that have nothing to do with the contacts table... I hope to god that makes sense because my head is spinning.

See code for my wrong example


INSERT INTO lead_tracking
 (ContactID)
 SELECT ContactID
 FROM contacts
 WHERE ContactStatusID =2

// in addition add a preset values of Lead_typeID = 2 & Lead_progressID = 5 ( into the lead_tracking  table)

Open in new window

0
Comment
Question by:jasch2244
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
8 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 1000 total points
ID: 36552232
If I'm understanding correctly, and you want to insert just hard code the values. This would insert the values 2 and 5 for all records.

INSERT INTO lead_tracking (ContactID, Lead_typeID, Lead_progressID )
SELECT ContactID, 2, 5
FROM    contacts
WHERE ContactStatusID =2

0
 
LVL 52

Expert Comment

by:_agx_
ID: 36552236
Gah ... that didn't make sense what I meant to say was :

"If I'm understanding correctly, just hard code the values. This would insert the values 2 and 5 for all records."
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 1000 total points
ID: 36552243
If this gives you the results you want:
SELECT contacts.ContactID,2,5
 FROM contacts
 WHERE contacts.ContactStatusID =2

backup your table and try:


INSERT INTO lead_tracking (ContactID,Lead_typeID,Lead_progressID)
 SELECT contacts.ContactID,2,5
 FROM contacts
 WHERE contacts.ContactStatusID =2
0
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
LVL 52

Expert Comment

by:_agx_
ID: 36553112
@jasch2244 - Did you miss my answer? I thought that's exactly what I suggested first.
0
 
LVL 1

Author Comment

by:jasch2244
ID: 36598549
Yeah, I'm not sure what happened there. I don't remember you having the INSERT INTO lead_tracking (ContactID,Lead_typeID,Lead_progressID)... I remember seeing " INSERT INTO lead_tracking" but not the later.

Sorry about that
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36599704
Ok, thanks.
0

Featured Post

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question