Showing entries 1 to 9
Displaying posts with tag: Join Tutorials (reset)
MySQL DELETE Join example

A very useful helper in your join toolbox can be a delete join. Even though it’s not a special join type but a join used within DELETE statements, it’s still worth mentioning. However, from time to time when I want to make use of delete joins  on my own, I somehow managed it to forgot the syntax and have to look it up somewhere. Therefor, here is a description as well as an example.

Take care: A delete join is a very powerful weapon. And with great power comes great responsibility! I recommend to develop the delete join on a development database. At least, make sure you have a working an recent backup before trying to delete things. A delete statement that uses joins makes it easy to shot yourself in the foot. And if you do, it probably blows away your hole leg.

 

DELETE join syntax

When you …

[Read more]
UPDATE joins in MySQL

When you want to update rows that are stored in MySQL tables you use update statements. Usually the table you’re updating has to be defined and in case you want to update only certain rows a condition for the update operation has to be specified. If the condition can be expressed based on columns of the same table, you most probably do this by adding a WHERE-clause. But what if there are rows in tableA which you want to update based on a condition that depends on related rows from tableB? The answer? Drumroll please…: Use a join as you would do in SELECT statements!

Syntax

When you’re updating data in MySQL, you can specify a condition to specify which rows should be affected. The following update join example makes use of an inner join. However, you can use any other join operation MySQL supports, like an outer …

[Read more]
MySQL INNER JOIN Tutorial: the comma operator

Here you find information about writing inner joins with the comma operator. It’s the most basic way to combine (join) two tables. There is an alternative syntax that can be used, because in MySQL you can write inner joins in two different ways. Another popular way is it to use the INNER JOIN command or synonymous keywords like CROSS JOIN and JOIN. Please make sure to read our dedicated documentation for more information when you understand the comma operator syntax.

Syntax

The following examples are equivalent to the INNER JOIN …

[Read more]
MySQL NATURAL JOIN Tutorial & Examples

This tutorial explains how you can use NATURAL JOINs and what a natural join is actually. Included are syntax details and example statements.

Generally speaking, the keyword NATURAL can be treated as a join condition which is added implicitly. If used, it replaces the keywords ON and USING altogether. In MySQL writing natural joins means adding the keyword NATURAL to either an INNER JOIN or an OUTER JOIN. Let’s take a look at how a natural join implies a join condition.

Syntax

First of all, some natural join syntax examples. As mentioned earlier, a natural join adds a implicit condition to inner and outer join statements:

-- natural inner …
[Read more]
MySQL STRAIGHT JOIN Tutorial & Examples

This tutorial shows you how to write straight joins in MySQL and when an it makes sense to do so. Included are a general description, syntax examples and a comparison of straight and inner joins.

.

Straight Join

A STRAIGHT_JOIN identifies and combines matching rows which are stored in two related tables. This is what an inner join also does. The difference between an inner join and a straight join is that a straight join forces MySQL to read the left table first.

To see if the STRAIGHT_JOIN hint would make sense, you have to use EXPLAIN to analyze your queries. When you feel that another join order could improve the performance, use a straight join.

Left table?

The STRAIGHT_JOIN keyword or hint, forces MySQL …

[Read more]
MySQL RIGHT OUTER JOIN Tutorial & Examples

Here you find information about writing RIGHT JOINs (also referred to as RIGHT OUTER JOINs). This introduction into right joins includes a detailed description, syntax information and right outer join example statements. The Venn diagram on the left represents a result set that a statement with a right join produces. Please refer to the syntax examples below for an example. Links to additional information resources can be found at the end of this article.


Right Join syntax

First of all, some syntax examples for the impatient:

-- right join with USING-clause
SELECT *
FROM <leftTable> RIGHT JOIN <rightTable>
USING(id)
-- right join with ON-clause
SELECT *
FROM <leftTable> a RIGHT JOIN <rightTable> b …
[Read more]
MySQL LEFT OUTER JOIN Tutorial & Examples

Here you find information about writing LEFT JOINs (also referred to as LEFT OUTER JOINs). This introduction into left joins includes a description, syntax information and example statements that use left outer joins. The Venn diagram on the left represents a result set that a statement with a left join produces. Please refer to the syntax examples below for an example. Links to additional information resources can be found at the end of this article.

Left Join syntax

First of all, some syntax examples for the impatient:

-- left join with USING-clause
SELECT *
FROM <leftTable> LEFT JOIN <rightTable>
USING(id)
-- left join with ON-clause
SELECT *
FROM <leftTable> a LEFT JOIN <rightTable> b
ON a.name = b.authorName

As you can see, a join condition can be written …

[Read more]
MySQL OUTER JOIN Tutorial & Examples

Here you find information about OUTER JOINs in MySQL statements. This tutorial is a general introduction. Syntax details and example statements are split into a LEFT OUTER JOIN tutorial and RIGHT OUTER JOIN tutorial because these are specific outer joins. 

Outer Join basics

MySQL supports outer joins. They identify matching rows that are stored in two different tables and add the selected columns to the result set. That’s exactly what an inner join does as well.

The difference between inner and outer join is: An outer join can identify rows without a match in the joined table. When …

[Read more]
MySQL INNER JOIN Tutorial & Examples

This tutorial shows you how to write ANSI-Style inner joins with the INNER JOIN keywords. Included are a general description, some syntax examples and a comparison between inner and cross joins.

Note: In MySQL the join keywords JOIN and CROSS JOIN are synonymous with INNER JOIN. That means: All example statements found in this article work fine when you replace INNER JOIN with JOIN or CROSS JOIN.


Syntax

Here are syntax examples for the impatient. Basically, ANSI-style join conditions can be specified with two different keywords: USING and ON. Take a look at the following examples:

-- inner join with USING clause
SELECT *
FROM <firstTable> a INNER JOIN <anotherTable> b
USING(<columnName>)
-- inner join with ON …
[Read more]
Showing entries 1 to 9