Jump to content

Trouble With Joins


Dfrtbx
 Share

Recommended Posts

Hello.For a website I'm making, in order to register, you must know someone that is already registered. Checking to make sure that the "referrer" they entered into the registration field is actually a user is easy enough:

SELECT firstName, lastName FROM users WHERE firstName = '$refFirstName' && lastName = '$refLastName';

Where $refFirstName and $refLastName are the referrer's first and last names, respectively.Here is the layout of the database.Because it is a "private" site (i.e. a limited user base), I was going to make a page that displays all the users and the person that referred them. Simply put, I have no idea how to do it. In the referrers table, refID is always equal to userID. I cannot figure out a way to get the NAME of a user's referrer out of the database in the same SELECT query as everything else. Would I use a LEFT JOIN or INNER JOIN, or something else? Also, is there anyway to combine the referrer table into the user one? Thanks!

Link to comment
Share on other sites

Hi, you already have the idea, but to get the names of the referrers you need two joinsSELECT *FROM users uINNER JOIN referrers r ON u.refID = r.refIDINNER JOIN users ur ON r.userID = ur.userIDusing the table alias you can call the same table more than one time, then the referres' names could use field alias, something like: ur.firstName AS r_firstName that allows to tell apart the name of the user and the referrer's

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...