This link has been bookmarked by 60 people . It was first bookmarked on 24 Apr 2007, by Kim Woodbridge.
-
14 Feb 17
-
19 May 14
-
25 Apr 13
-
17 Apr 13
-
23 Sep 12
-
17 Feb 12
-
21 Jun 11
-
a join is. Basically it is the combining of two rows based on the comparative values in selected columns
-
A cross-join between two tables takes the data from each row in table1 and joins it to the data from each row in table2
-
mysql> SELECT cds.artist, genres.genre -> FROM cds, genres;
-
In the equi-join the comparison we are making between two columns is that they match the same value. We can use this method to select certain fields from both tables and only the correct rows will be joined together.
-
mysql> SELECT cds.artist, cds.title, genres.genre -> FROM cds, genres -> WHERE (cds.genreID = genres.genreID);
-
The left join is a mechanism used to join tables before we add other conditions such as WHERE etc.
-
mysql> SELECT cds.artist, cds.title, genres.genre -> FROM cds -> LEFT JOIN genres -> ON cds.genreID = genres.genreID;
-
An important thing to note with this particular join is that even if there are no records in the second table (in this case 'genres') data will still be displayed from the first table.
-
a RIGHT JOIN which is a variation where all the data on the RIGHT side of the join (the second table) is returned regardless of the presence of data from the first table.
-
SELECT cds.artist, cds.title, genres.genreID, genres.genre -> FROM cds -> RIGHT JOIN genres -> ON cds.genreID = genres.genreID;
-
every record of the RIGHT side must be returned at least once by the RIGHT JOIN.
-
SELECT cds.artist, cds.title, genres.genre -> FROM cds -> LEFT JOIN genres -> ON cds.genreID = genres.genreID -> WHERE genres.genre = 'pop';
-
use this if the columns you are carrying out the join on have the same name.
-
SELECT <column_name> FROM <Table1> LEFT JOIN <Table2> USING (<column_name>)
-
SELECT cds.artist, cds.title, genres.genre -> FROM cds -> LEFT JOIN genres -> USING (genreID);
-
We have a cds table that contains the foreign keys (i.e. values that correspond to primary keys in another table) called cds.genreID and cds.artistID which also exist in the genres and artists tables. A three table join can be achieved using another version of the Equi-Join or Inner Join where we can use the WHERE clause to limit the returned records based on comparing the artistID and the genreID.
-
the first JOIN creates a virtual table (from joining tables one and two) which can then be joined to the third table.
-
SELECT artists.Artist, cds.title, genres.genre -> FROM cds -> LEFT JOIN genres -> ON cds.genreID = genres.genreID -> LEFT JOIN artists -> ON cds.artistID = artists.artistID -> WHERE (genres.genre = 'Pop')
-
SELECT artists.Artist, cds.title, label.Label, genres.genre -> FROM cds -> LEFT JOIN genres -> ON cds.genreID = genres.genreID -> LEFT JOIN artists -> ON cds.artistID = artists.artistID -> LEFT JOIN label -> ON cds.labelID = label.labelID;
-
This method of adding tables and performing JOINS will work only if one TABLE has all the foreign keys.
-
UPDATE cds->LEFT JOIN artists -> ON cds.artistID = artists.artistID -> SET -> cds.title = 'A Funk Odyssey', -> artists.name = 'Jamiroquai' -> WHERE (cds.cdID = '2');
-
DELETE cds, artist -> FROM cds, artist -> WHERE (cds.artistID = artists.artistID) -> AND (cds.artistID = '3');
-
-
14 Jun 11
-
left join is a mechanism used to join tables before we add other conditions such as WHERE etc.
-
Syntax:
SELECT <column_name> FROM <Table1> LEFT JOIN <Table2> ON Table1.column = Table2.column
-
important thing to note with this particular join is that even if there are no records in the second table (in this case 'genres') data will still be displayed from the first table
-
data from the LEFT of the join will be displayed
-
mysql> SELECT cds.artist, cds.title, genres.genre -> FROM cds -> LEFT JOIN genres -> ON cds.genreID = genres.genreID -> WHERE genres.genre = 'pop';
-
can use this if the columns you are carrying out the join on have the same name.
-
'USING' clause
-
joining the tables where cds.genreID is the same as genres.genreID
-
genreID is the name of a column in BOTH of tables we are using for the join
-
mysql> SELECT cds.artist, cds.title, genres.genre -> FROM cds -> LEFT JOIN genres -> USING (genreID);
-
possible to join more than two tables
-
Joining Three Tables
-
have a cds table that contains the foreign keys (i.e. values that correspond to primary keys in another table) called cds.genreID and cds.artistID which also exist in the genres and artists tables
-
use the WHERE clause to limit the returned records based on comparing the artistID and the genreID.
-
problem
-
already have a fairly complex WHERE clause
-
Ideally what we want is to have a LEFT / RIGHT JOIN
-
solution to this is to use a series of joins
-
result is passed to a second join
-
mysql> SELECT artists.Artist, cds.title, genres.genre -> FROM cds -> LEFT JOIN genres -> ON cds.genreID = genres.genreID -> LEFT JOIN artists -> ON cds.artistID = artists.artistID -> WHERE (genres.genre = 'Pop');
-
'label' table
-
could also join a fourth table
-
statement to join all four tables and display the results.
-
SELECT artists.Artist, cds.title, label.Label, genres.genre -> FROM cds -> LEFT JOIN genres -> ON cds.genreID = genres.genreID -> LEFT JOIN artists -> ON cds.artistID = artists.artistID -> LEFT JOIN label -> ON cds.labelID = label.labelID;
-
-
04 May 11
-
05 Apr 11
-
06 Feb 11
lihao suncolumn
-
19 Nov 10
-
18 Nov 10
-
29 Sep 10
-
09 Sep 10
-
04 Jun 10
-
29 Apr 10
-
19 Apr 10
-
09 Mar 10
-
08 Feb 10
-
21 Dec 09
-
09 Dec 09
-
This is why we have been prefixing the column name with the table name to ensure we can tell the difference
-
This 'super-row' exists only for the duration of the query that creates it
-
A variation on the Left Join is the 'USING' clause.
-
rather than being used to evaluate and display the data from it, the result is passed to a second join
-
A good way to think of this is that the first JOIN creates a virtual table (from joining tables one and two) which can then be joined to the third table.
-
-
16 Oct 09
Michael GurnettSELECT cds.artist, cds.title, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> ON cds.genreID = genres.genreID; -
06 Oct 09
-
03 Jun 09
-
18 May 09
-
14 Apr 09
-
21 Jan 09
-
16 Jan 09
-
08 Oct 08
-
27 May 08
Ian McGregorSELECT <column_name> FROM <Table1>, <Table2> WHERE (Table1.column = Table2.column)
-
09 May 08
-
08 May 08
-
02 Apr 08
-
| westlife | westlife | NULL
-
-
04 Mar 08
-
04 Oct 07
-
12 Jul 07
-
13 Apr 07
-
17 Mar 07
-
14 Mar 07
-
22 Aug 06
-
08 Aug 06
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.