Reputation: 13641
I have this SQL query, and directly under it, another query based on the result. I'd like to merge them, but im getting confused, being a newbie.
It's pulling data from 3 tables- the first query from the USERS and the PLAYERS table. Then if the player is in a 'team', it gets the tag from the TEAMS table.
My code:
$sql_result4=mysql_query("SELECT * FROM users JOIN players ON users.player=players.name WHERE users.id='$id'");
$rs4 = mysql_fetch_array($sql_result4);
if ($rs4[team] !=0) {
$sql_result = mysql_query("SELECT tag FROM teams WHERE id='$rs4[team]'", $db);
$rs = mysql_fetch_array($sql_result);
// get data
}
My question is, how could i merge that second query in with the first? Am I even using the right kind of join?
I want to be able to use lots of data from the PLAYERS table, but only one or two columns from the USERS and TEAMS ones.
Upvotes: 0
Views: 185
Reputation: 36999
Just use a left join.
SELECT ...
FROM users u
JOIN players p ON u.player = p.name
LEFT JOIN teams t ON p.team = t.id
WHERE u.id = ?
Upvotes: 1