Link to home
Start Free TrialLog in
Avatar of Michael Robinson
Michael RobinsonFlag for United States of America

asked on

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
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

 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
ASKER CERTIFIED SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michael Robinson

ASKER

i think that's working
will try more after lunch

many thanks

michael
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


User generated image