Sunday, August 3, 2008

A MySQL concept I found difficult to understand


These two MySQL queries return the same value:



select photographer_lname, image_title

from photographer

right join image

on photographer.photographer_id=image.photographer_id;




select photographer_lname, image_title

from image

left join photographer

on photographer.photographer_id=image.photographer_id;



In the first query, the right join returned all the rows from the second table (image), even if there was no value from the first table (photographer.)

In the second query, the left join returned all the rows from the first table (image), even if there was no value from the second table (photographer.)

Left and right refer to the first and second tables. Left- first table, right- second table.


Below, this is the general example for all rows returned, from the table in CAPS:

select column1, column2

from TABLE1

left join table2


select column1, column2

from table1

right join TABLE2

No comments: