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
bigmikey88Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.