i have this query,
$query = 'SELECT * FROM users where id ="'.$_SESSION['user_id'].'"';
My problem is that i don’t know how to left join it with another table..using another where or “On” clause..
can you please help me with the syntax of this?
It should be something like SELECT $query FROM users,retailer ON $query.user_id=retailer.user_id
….
"SELECT users.first_name, retailer.date
FROM users AS users
LEFT JOIN retailer AS retailer ON users.user_id=retailer.user_id
WHERE users.user_id=" . $_SESSION['user_id']
Thanks!
LEFT JOIN is pretty simple
For your example:
“AS” keyword creates alias so there is no need of making alias “users” of table “users”.
Secondly, LEFT JOIN is useful if there is a chance that second table contains no matches to first one yet you still need that attribute to show – attributes of second table will have NULL values if no matches found.
I’m not really sure you need a LEFT JOIN in your query.