• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 63
  • Last Modified:

How do I join these two separate databases in ColdFusion

Hi,

How do I join these two databases?

db1: Visitors
Table: Users
This table contains all users on the system.  each one has a unique "UserID"


db2:  PT_UserData
Table: Client_Scores
This table contains only those users who have completed a test
If they complete a test, the field "YourType" IS NOT NULL
All rows contain the field "UserID"


Both tables have "UserID" in common

I'm looking to output those users who have completed the test, but I need their Email and LastName which is in the Users table
0
bigmikey88
Asked:
bigmikey88
  • 3
1 Solution
 
NerdsOfTechTechnology ScientistCommented:
 SELECT u.`email`, u.`LastName` 
 FROM Client_Scores cs
 LEFT JOIN Users u
 ON cs.UserID = u.UserID

Open in new window


Since a user with no score record would mean they haven't taken the test, return only those who have taken the test and matching user
0
 
NerdsOfTechTechnology ScientistCommented:
 SELECT u.`email`, u.`LastName` 
 FROM Client_Scores cs
 LEFT JOIN Users u
 ON cs.UserID = u.UserID
 WHERE cs.YourType IS NOT NULL

Open in new window


unless the user can have a score without taking the test, I think the WHERE might be unnecessary .
0
 
bigmikey88Author Commented:
i think that's working
will try more after lunch

many thanks

michael
0
 
NerdsOfTechTechnology ScientistCommented:
Thanks for the points!

I was very tempted to use the rare RIGHT JOIN syntax since the SELECT fields where from Users and it might have looked nicer being that the fields would have come from the FROM table; however, I figured LEFT JOIN would be most acceptable. In case you wanted to see the alternate form:

 SELECT u.`email`, u.`LastName` 
 FROM Users u
 RIGHT JOIN Client_Scores cs
 ON u.UserID = cs.UserID 
 WHERE cs.YourType IS NOT NULL

Open in new window


Lastly, and possibly the BEST syntax in your case, would be as follows: if a Client_Score exists for a User that doesn't exist (say a deleted user) the other queries would return such an orphaned record; to prevent this issue, you could use an INNER JOIN as an alternative (to require that a User AND a Score records exist).

(Recommended)

 SELECT u.`email`, u.`LastName` 
 FROM Users u
 INNER JOIN Client_Scores cs
 ON u.UserID = cs.UserID 
 WHERE cs.YourType IS NOT NULL

Open in new window


sql-joins
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now