JOIN two queries

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

JOIN two queries

Post by SidewinderX »

I have two queries. The first query gets a single piece of data from the table `users` then the second query uses the data from the first query to grab new information. I believe the JOIN mechanism is used to combined these two queries into a single query, but I can't figure out the syntax for the life of me. If someone could give me a hand I'd appreciate it. Thanks.

Code: Select all

$q = "SELECT * FROM users WHERE username='$username'";
$result = mysql_query($q) or die ('Something is wrong with query: ' . $q . '<br>'. mysql_error());
$row = mysql_fetch_assoc($result);
$pcid = $row['pcid'];

$q = "SELECT * FROM bhd_stats WHERE pcid='$pcid'";
$result = mysql_query($q) or die ('Something is wrong with query: ' . $q . '<br>'. mysql_error());
$row = mysql_fetch_assoc($result);
//echo other stuff
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

There's a brief tutorial at http://www.w3schools.com/sql/sql_join.asp
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

Not to be an ass, but I know... I can google too :roll:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

And which part of the tutorial do you not understand?
Take a closer look at the section "Referring to Two Tables".
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

viola

Code: Select all

$q = "SELECT * FROM bhd_stats, users WHERE bhd_stats.pcid=users.pcid AND users.username='$username'";
thanks :?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you're welcome :roll:
Post Reply