IT Community - Software Programming, Web Development and Technical Support

MySQL Joins

This is a discussion on MySQL Joins within the Database Support forums, part of the Web Development category; Joins are an integral part of a relational database. Joins allow the database user to take advantage of the relationships ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 08-21-2008, 04:35 PM
rajinikanth rajinikanth is offline
Super Moderator
 
Join Date: Mar 2007
Posts: 8
rajinikanth is on a distinguished road
Default MySQL Joins

Joins are an integral part of a relational database. Joins allow the database user to take advantage of the
relationships that were developed in the design phase of the database. A join is the term used to describe the act when two or more tables are "joined" together to retrieve needed data based on the relationships that are shared between them. For example, in your Meet_A_Geek database, you have a relationship between the Customers and Orders tables. To select data out of the Customers table based on some criterion that is contained in the Orders table would require you to use a join of some kind. For example, to retrieve all the customers who have placed an order, you could use the following syntax:
SELECT First_Name, Last_Name
FROM Customer AS C, Orders AS O
WHERE C.Customer_ID = O. Customer_ID
ANSI-92 Compliance
__________________
- D.Rajinikanth
**Born to Shine**
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-21-2008, 04:35 PM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: MySQL Joins

CROSS JOIN
The CROSS JOIN is not used very much at all. In fact, most of the time it is done by mistake. A CROSS JOIN returns all the records from all the tables mentioned in the JOIN. This is also referred to as a Cartesian join]. These joins are very processor-intensive and should be avoided. The syntax for a CROSS JOIN would look like the following:
The old way:

SELECT C.First_Name, C.Last_Name, O.Order_ID FROM Customers as C, Orders as O
The ANSI-92 way:

SELECT C.First_Name, C.Last_Name, O.Order_ID FROM Customers as C CROSS JOIN Orders as O

The resultset that is returned from this type of join is huge. It basically returns all the data in all the tables.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-21-2008, 04:35 PM
velhari velhari is offline
D-Web Programmer
 
Join Date: Mar 2007
Location: Chennai
Posts: 67
velhari is on a distinguished road
Send a message via AIM to velhari
Default Re: MySQL Joins

INNER JOIN.
INNER JOINs are probably the most common of all joins.
An INNER JOIN simply means that all records that are unmatched are discarded. Only the matched rows are displayed in the resultset. This is the default type of join, so the word INNER is optional. This type of join is based on the criteria in the JOIN clause. Your first example was an example of an INNER JOIN:
SELECT C.First_Name, C.Last_Name, O.Order_ID
FROM Customer as C
(INNER) JOIN Orders as O ON C.Customer_ID = O.Customer_ID


Again, the word INNER appears in parentheses because it is optional. The old way to create an INNER
JOIN would look like the following:

SELECT First_Name, Last_Name, O.Order_ID FROM Customer as C, Order as
O WHERE C.Customer_ID = O.Customer_ID


This is probably the most frequently used of all joins—the primary reason why it's the ANSI-92 default.
__________________
Regards,
VELHARI
I am not totally useless. I can be used for a bad example
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-21-2008, 04:35 PM
senthilkannan senthilkannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 324
senthilkannan is on a distinguished road
Default Re: MySQL Joins

LEFT JOIN
A LEFT JOIN returns all rows from the left table in a join. For example, if you were to continue using your Customers and Orders example, the following statement shows the syntax for a LEFT JOIN:

SELECT C.First_Name, C.Last_Name, O.Order_ID FROM Customer as C LEFT JOIN Order as O ON C.Customer_ID = O.Customer_ID

In this example, all the rows from the table on the left side of the equation will be returned, regardless of whether they have a match with the table on the right side. If there is no match, a NULL value will be returned. In other database systems, the LEFT JOIN is implemented the "old way" using a symbol. The symbol is an asterisk (*) equal sign (=) combination. The placement of the asterisk (left or right of the equal sign) indicates the type of join. So, to implement a LEFT JOIN, the following would be the syntax:
SELECT C.First_Name, C.Last_Name, O.Order_ID FROM Customers AS C, Orders as O WHERE C.Customer_ID *= O.Orders_ID
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-21-2008, 04:35 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: MySQL Joins

NATURAL LEFT JOIN
The NATURAL LEFT JOIN is the same as a regular LEFT JOIN except that it automatically uses all the matching columns as part of the join. It is syntactically equivalent to a LEFT JOIN with a USING clause that names all the identical columns of the two tables. The syntax looks like the following:

SELECT C.First_Name, C.Last_Name, O.Orders_ID
FROM Customers as C
NATURAL LEFT JOIN Orders as O


This would return all the rows from the Customers table, regardless of whether they had a matching record in any of the same columns of the Orders table. The Orders table would return a NULL if it did not have a match. Again, this join is rarely used. The only way to accomplish this type of join is to use the given syntax. Joins are fairly straightforward, But don't let the syntax fool you. Joins allow you to take advantage of the relationships you defined earlier when you created the tables. Practice and experience will aid greatly when creating joins
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 10-11-2008, 03:37 AM
jackjack jackjack is offline
D-Web Trainee
 
Join Date: Sep 2008
Posts: 12
jackjack is on a distinguished road
Default Re: MySQL Joins

In this Virtual Workshop we will look at retrieving data from a relational database structure, i.e. with multiple tables, using the SQL JOIN Syntax.

jack
_________________________________________

steripen handheld Girls Games
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I Can Help With MYSQL theseokit Database Support 8 09-05-2007 06:01 AM


All times are GMT -7. The time now is 06:20 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0