Page 1 of 1

Query fromm 2 tables ( JOIN ??)

Posted: Wed Aug 25, 2010 11:59 am
by diseman
Hello Experts,

My first query where I'm trying to show the results from two separate tables (users & contact_info). I've spent a few hours Google'ing and attempting, but no luck. I think it's time to ask for help.

Basically, I can get all the information I need onto this page EXCEPT for 'b_lastname,' which is stored in the other table (contact_info).

UPFRONT:

Two tables (users, contact_info),
2 existing records (one where 'usertype=5' or admin),
data I need from users table (shown in query below),
data I need from contact_info table is -> 'b_lastname'

Here's what I have:

Code: Select all


$query = 	"SELECT	id, file_status, affiliate, start_date, complete_date ".
 		"FROM 	users " .
		"WHERE 	usertype != '5'";
I realize I don't have 'b_lastname' or 'contact_info' in the querry above, but that's because everything I had was a mess and didn't work, so I didn't see any point in putting there to confuse anything.

So, the 4 bits of data in the query above are displayed for me in my page, so that works. I simply can't get the last name to show without duplicating the results. Meaning, the way I've been trying I always get 2 records; one with a last name and one without.

I'm assuming this is a JOIN situation, but I couldn't get it to work. I also tried a nested query, but that gave me a headache.

Hoping I've explained it well enough and kept it simple and someone can help.

Thank you in advance...

Re: Query fromm 2 tables ( JOIN ??)

Posted: Wed Aug 25, 2010 1:00 pm
by diseman
Well, it would seem I finally stumbled on a example that helped me get through it this time. Thanks to anyone who may be putting something together as I write this.

Here's what I have for others looking for help:

Code: Select all

$query = "SELECT users.username, users.file_status, users.affiliate, users.start_date, users.complete_date, contact_info.b_lastname
 		FROM users
		INNER JOIN contact_info
		ON users.username = contact_info.username 
		WHERE users.usertype !='5' " ;
		

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){