When you use left join OR right join there is really no "need" for a "where" statement when you use normal join use WHERE but not when you do a left or right join. Syntax could be like this for a left join:
SELECT people.firstname, cars.whatcar FROM people LEFT JOIN cars ON people.p_id = cars.p_id
What this code first does is declares what it tries to SELECT. in this case information from two tables: people.firstname and cars.whatcar (to be more precise the firstname from people table and the whatcar info from cars table).
Then comes the join: FROM people LEFT JOIN cars
What this does it that it selects TWO tables to work against (one being people and the other being cars). Focus are however on the left side in a left join, here you can see that PEOPLE table are on the left and CARS on the right, the difference is explained further down but note what is on the left and right.
What the next row does:
ON people.p_id = cars.p_id
Is that it sets a conditional statment to to JOINING function. The firstname where people.p_id exists will be selected no matter what. And then the conditional statement is checked to see if there is a cars.p_id and if it does that info comes along as well. But the cars.p_id could very well be empty where a people.p_id exists and basically you will get a return containing firstname but a NULL value for the second entry.
Lets imagine we got those people in our people table:
p_id = 1;
name = "joe";
p_id = 2;
name = "mary";
p_id = 3;
name = "quirk";
- p_id = 1;
- name = "joe";
- p_id = 2;
- name = "mary";
- p_id = 3;
- name = "quirk";
And this in our cars table:
p_id = 1;
name = "Peugeot 308";
p_id = 2;
name = "Mercedes E63";
p_id = 4;
name = "Saab";
- p_id = 1;
- name = "Peugeot 308";
- p_id = 2;
- name = "Mercedes E63";
- p_id = 4;
- name = "Saab";
Our select statement in the beginning would give us this:
joe owns the Peugeot 308, mary owns the Mercedes E63, quirk owns (NULL). Basically you get a return on quirk despite he don't own a car (this could be one very useful aspect of left/right joins). If you want to check out the status of all cars (who got a owner and what car has no owner) you can do one of the following:
SELECT people.firstname, cars.whatcar FROM people RIGHT JOIN cars ON people.p_id = cars.p_id
Focus is now on the right table and the return would be as follows:
Peugeot 308 owner joe, Mercedes E63 owner mary, Saab owner (NULL). The person quirk isn't even mentioned, focus is now on the right side (CARS).
SELECT people.firstname, cars.whatcar FROM cars LEFT JOIN people ON people.p_id = cars.p_id
Basically keeping it a left join and just switch place with cars and people in our join statement!
A normal join:
SELECT people.firstname, cars.whatcar FROM cars LEFT JOIN people WHERE (people.p_id = cars.p_id)
would return ONLY the stuff where there is a people.p_id AND a cars.p_id that matches. Basically no NULL values. This is what would returned from our JOIN example: joe owns the Peugeot 308, mary owns the Mercedes E63.
I'm not sure if this was helpful or correct at all points. I got my first JOINS working a few days ago! : P