0

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); 

4 Answers 4

1
$sql="SELECT * FROM accounts JOIN players ON accounts.accounts_link_to_player_id_here=players.id WHERE accounts.id='$val'";
Sign up to request clarification or add additional context in comments.

1 Comment

Could you mark the question answered for future Googlers? Thanks!
1

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';

Comments

0

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

Comments

-1

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' )"

1 Comment

A subselect is a bad way to do this for several reasons, a join is more than suffice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.