JOIN Syntax

MySQL supports the following JOIN syntaxes for the table_references part of SELECT statements and multiple-table DELETE and UPDATE statements:

table_references:
table_reference, table_reference
| table_reference [INNER | CROSS] JOIN table_reference [join_condition]
| table_reference STRAIGHT_JOIN table_reference
| table_reference LEFT [OUTER] JOIN table_reference join_condition
| table_reference NATURAL [LEFT [OUTER]] JOIN table_reference
| { OJ table_reference LEFT OUTER JOIN table_reference
ON conditional_expr }
| table_reference RIGHT [OUTER] JOIN table_reference join_condition
| table_reference NATURAL [RIGHT [OUTER]] JOIN table_reference

table_reference:
tbl_name [[AS] alias] [index_hint)]

join_condition:
ON conditional_expr
| USING (column_list)

index_hint:
USE {INDEX|KEY} (index_list)]
| IGNORE {INDEX|KEY} (index_list)]
| FORCE {INDEX|KEY} (index_list)]

index_list:
index_name [, index_name] …

Index hints can be specified to affect how the MySQL optimizer makes use of indexes. For more information, see Section 13.2.7.2, “Index Hint Syntax”.

Note that several changes in join processing were made in MySQL 5.0.12 to make MySQL more compliant with standard SQL. These changes include the ability to handle nested joins (including outer joins) according to the standard. If a nested join returns results that are not what you expect, please consider upgrading to MySQL 5.0. Further details about the changes in join processing can be found at http://dev.mysql.com/doc/refman/5.0/en/join.html.

You should generally not have any conditions in the ON part that are used to restrict which rows you want in the result set, but rather specify these conditions in the WHERE clause. There are exceptions to this rule.

Note that INNER JOIN syntax allows a join_condition only from MySQL 3.23.17 on. The same is true for JOIN and CROSS JOIN only as of MySQL 4.0.11.

The { OJ … LEFT OUTER JOIN …} syntax shown in the preceding list exists only for compatibility with ODBC. The curly braces in the syntax should be written literally; they are not metasyntax as used elsewhere in syntax descriptions.

MySQL - LEFT JOIN and RIGHT JOIN, INNER JOIN and OUTER JOIN
In a database such as MySQL, data is divided into a series of tables (the “why” is beyond what I’m writing today) which are then connected together in SELECT commands to generate the output required. I find when I’m running MySQL training, people often get confused between all the join flavours. Let me give you an example to see how it works.

If this isn’t quite the question you’re looking to have answered, we’ve got a MySQL IAQ (Infrequently Answered Questions that may help you

First, some sample data:
Mr Brown, Person number 1, has a phone number 01225 708225
Miss Smith, Person number 2, has a phone number 01225 899360
Mr Pullen, Person number 3, has a phone number 01380 724040
and also:
Person number 1 is selling property number 1 - Old House Farm
Person number 3 is selling property number 2 - The Willows
Person number 3 is (also) selling property number 3 - Tall Trees
Person number 3 is (also) selling property number 4 - The Melksham Florist
Person number 4 is selling property number 5 - Dun Roamin.

mysql> select * from demo_people;

+————+————–+——+

| name | phone | pid |

+————+————–+——+

| Mr Brown | 01225 708225 | 1 |

| Miss Smith | 01225 899360 | 2 |

| Mr Pullen | 01380 724040 | 3 |

+————+————–+——+

3 rows in set (0.00 sec)

mysql> select * from demo_property;

+——+——+———————-+

| pid | spid | selling |

+——+——+———————-+

| 1 | 1 | Old House Farm |

| 3 | 2 | The Willows |

| 3 | 3 | Tall Trees |

| 3 | 4 | The Melksham Florist |

| 4 | 5 | Dun Roamin |

+——+——+———————-+

5 rows in set (0.00 sec)

mysql>

If I do a regular JOIN (with none of the keywords INNER, OUTER, LEFT or RIGHT), then I get all records that match in the appropriate way in the two tables, and records in both incoming tables that do not match are not reported:

mysql> select name, phone, selling

from demo_people join demo_property

on demo_people.pid = demo_property.pid;

+———–+————–+———————-+

| name | phone | selling |

+———–+————–+———————-+

| Mr Brown | 01225 708225 | Old House Farm |

| Mr Pullen | 01380 724040 | The Willows |

| Mr Pullen | 01380 724040 | Tall Trees |

| Mr Pullen | 01380 724040 | The Melksham Florist |

+———–+————–+———————-+

4 rows in set (0.01 sec)

mysql>

If I do a LEFT JOIN, I get all records that match in the same way and IN ADDITION I get an extra record for each unmatched record in the left table of the join - thus ensuring (in my example) that every PERSON gets a mention:

mysql> select name, phone, selling

from demo_people left join demo_property

on demo_people.pid = demo_property.pid;

+————+————–+———————-+

| name | phone | selling |

+————+————–+———————-+

| Mr Brown | 01225 708225 | Old House Farm |

| Miss Smith | 01225 899360 | NULL |

| Mr Pullen | 01380 724040 | The Willows |

| Mr Pullen | 01380 724040 | Tall Trees |

| Mr Pullen | 01380 724040 | The Melksham Florist |

+————+————–+———————-+

5 rows in set (0.00 sec)

mysql>

If I do a RIGHT JOIN, I get all the records that match and IN ADDITION I get an extra record for each unmatched record in the right table of the join - im my example, that means that each property gets a mention even if we don’t have seller details:

mysql> select name, phone, selling

from demo_people right join demo_property

on demo_people.pid = demo_property.pid;

+———–+————–+———————-+

| name | phone | selling |

+———–+————–+———————-+

| Mr Brown | 01225 708225 | Old House Farm |

| Mr Pullen | 01380 724040 | The Willows |

| Mr Pullen | 01380 724040 | Tall Trees |

| Mr Pullen | 01380 724040 | The Melksham Florist |

| NULL | NULL | Dun Roamin |

+———–+————–+———————-+

5 rows in set (0.00 sec)

mysql>

An INNER JOIN does a full join, just like the first example, and the word OUTER may be added after the word LEFT or RIGHT in the last two examples - it’s provided for ODBC compatability and doesn’t add an extra capabilities.

 




Leave a Reply



Related Posts

Information, Software, Technology



Site Navigation

  • zyban how to use
  • what is aspirin
  • viagra shop
  • right heart failure
  • atenolol withdrawal
  • chronic headache pain relief remedy
  • estradiol pills
  • pharmacy zocor
  • purchase generic viagra
  • alcoholism information treatment
  • mirtazapine depression
  • online pharmacy and online prescription
  • lexapro effects
  • depression drug treatment options
  • online diet drugs
  • relief of anxiety
  • high blood pressure natural cures
  • dog infections
  • osteoporosis therapy
  • buying prescription drugs online
  • medicine to reduce heart rate
  • on line meds
  • latest diet pill
  • what is amoxicillin
  • fastest way to lower blood pressure
  • rheumatoid arthritis medications
  • antidepressant citalopram
  • carisoprodol purchase
  • immune system facts
  • bacterial infection treatment
  • cat infections
  • prednisone drug
  • canadian drugs online
  • cholesterol disorder
  • at home acne treatment
  • effects of high blood pressure
  • medications for epilepsy
  • treatment for aids
  • upper back pain
  • over weight dog
  • drug norvasc
  • drug trileptal
  • health problems cats
  • smoking treatments
  • discount soma online
  • diet and weight loss
  • new arthritis medication
  • dog med
  • control appetite
  • high amount of acid in blood
  • where to order soma
  • drugs used in cancer treatments
  • healthy dog treat recipes
  • cheap cialis buy pharmacy online now
  • exercise for weight loss
  • cat health help
  • aids for sleeping
  • keep erections longer
  • diflucan oral
  • wellbutrin cost
  • bone loss treatments
  • antibiotics safe for dogs
  • atenolol interaction
  • cholesterol canada
  • cure bronchitis
  • breast cancer support
  • singulair generic
  • discount weight loss prescription
  • drug smoking stop
  • drug generic lipitor
  • diet for high blood pressure
  • zoloft order
  • weight loss support groups
  • removing dark spots from face
  • drugs for swelling
  • viagra in australia
  • stroke treatment
  • buy diazepam online
  • back pain products
  • body fat lose
  • cialis softgel
  • natural antibiotic anti-biotic
  • hair loss products that really work
  • gabapentin medication
  • the vitamin shop
  • how to buy viagra
  • health benefit of stop smoking
  • medication shingles
  • loratadine claritin
  • gaining muscle mass
  • acne
  • cheapest cialis online
  • epilepsy treatment
  • drug list high blood pressure
  • weight loss tips
  • discount cat and dog products
  • buy weight loss medications
  • cialis 30mg
  • cialis online softtabs
  • benicar dosage
  • carisoprodol cheapest
  • womens viagra
  • relief pain
  • osteoporosis arthritis
  • generics cialis
  • acomplia online
  • all natural breast enhancer
  • best hair loss treatment
  • cheap retin a
  • snoring woman
  • women body building
  • xenical pill
  • improve skin
  • what is clomid
  • dog calming pills
  • chronic snoring
  • how do you prevent bone loss
  • stop stroke
  • dog med
  • drug phenergan
  • muscle spasm relief
  • type two diabetes
  • woman hair loss remedy
  • antibiotic medicine
  • levitra dosing
  • levitra use
  • pain medications generic side effects
  • ideal pet products
  • health problems in dogs
  • weight loss pills for women
  • healing wrinkles
  • zyprexa 5mg
  • buy viagra order viagra
  • pet meds
  • how do diuretics lower blood pressure
  • loratadine claritin
  • what are pain pills prescribed for
  • list of cancer treating drugs
  • how to treat prostate cancer
  • medicament
  • effexor side effects
  • treatment of hypertension
  • parkinson's disease medication
  • anti depression drugs
  • treatment of type 1 diabetes
  • list all vitamins
  • canada women health
  • lexapro medication
  • female health problem
  • order on line medicine without prescriptions
  • black spots on the face
  • reduce blood pressure
  • discount prescription medicine
  • medicine for depression
  • buy viagra online at
  • osteoporosis and men
  • severe pain medication
  • viagra tablet
  • buy birth control pills
  • cvs pharmacy
  • online ed drugs
  • arthritis joint pain
  • stopping hair loss
  • lexapro drugs
  • carisoprodol dosage
  • buy erectile dysfunction online
  • child diabetes
  • buying medication
  • fda plavix
  • hoodia pill
  • cialis discount generic
  • pharmacies without prescriptions
  • longer lasting erection
  • skin spots
  • little penis
  • medications without a prescription
  • no prescription meds next day
  • cheap drug prices
  • viagra in mexico
  • care hair loss
  • weight loss cures
  • accutane generic
  • pain meds and pregnancy
  • natural remedies for menopause
  • senna constipation
  • treatment of congestive heart failure
  • list a antibiotics
  • cure edema
  • antibiotic substitute
  • latest breast cancer drugs
  • constipation medications
  • high blood pressure women
  • information on cholesterol
  • health support
  • treatment of high blood pressure
  • weight loss principles
  • buy soma online
  • new depression drugs
  • depression relief
  • omega fish oil