Page 1 of 1

JOIN two queries

Posted: Sun Jul 08, 2007 7:46 pm
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

Posted: Sun Jul 08, 2007 7:54 pm
by volka
There's a brief tutorial at http://www.w3schools.com/sql/sql_join.asp

Posted: Sun Jul 08, 2007 8:23 pm
by SidewinderX
Not to be an ass, but I know... I can google too :roll:

Posted: Sun Jul 08, 2007 8:27 pm
by volka
And which part of the tutorial do you not understand?
Take a closer look at the section "Referring to Two Tables".

Posted: Sun Jul 08, 2007 8:38 pm
by SidewinderX
viola

Code: Select all

$q = "SELECT * FROM bhd_stats, users WHERE bhd_stats.pcid=users.pcid AND users.username='$username'";
thanks :?

Posted: Sun Jul 08, 2007 8:41 pm
by volka
you're welcome :roll: