user961882
user961882

Reputation: 237

php mysql join query

I need to pull data from 2 tables in my database. The data I pull from table 2 depends on the result of table 1.

I'm not amazing at all these JOINS and things, so if someone could just explain what kind of a JOIN i'd need here, and how it would look, i'd be grateful:

$sql_result = mysql_query("SELECT * FROM accounts WHERE id='$val'", $db); 
$rs = mysql_fetch_array($sql_result); $name = $rs[name];

$sql_result2 = mysql_query("SELECT * FROM players WHERE name='$name'", $db); 
$rs2 = mysql_fetch_array($sql_result2); 

Upvotes: 0

Views: 293

Answers (4)

zion ben yacov
zion ben yacov

Reputation: 725

SELECT * FROM accounts LEFT JOIN players USING (name) WHERE accounts.id = 'value';

Upvotes: 0

Rudi Visser
Rudi Visser

Reputation: 21979

You can do something like this, depending on the structure of the table(s):

SELECT * FROM `accounts` INNER JOIN `players` USING (`name`) WHERE `accounts`.`id` = 'value';

Upvotes: 1

Mike Neugebauer
Mike Neugebauer

Reputation: 33

You'll need a query that looks like this (this is known in SQL as a subselect):

"SELECT p.* FROM players p WHERE name IN ( SELECT n.name FROM accounts n WHERE n.id = '$val' )"

Upvotes: -1

Whetstone
Whetstone

Reputation: 1199

$sql="SELECT * FROM accounts JOIN players ON accounts.accounts_link_to_player_id_here=players.id WHERE accounts.id='$val'";

Upvotes: 1

Related Questions