MySQL RIGHT JOIN

MySql Right Join

MySql Right Join

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. refer to the syntax examples below for an example.

Right Join syntax
First of all, some syntax examples for the impatient:

— right join with USING-clause
SELECT *
FROM RIGHT JOIN
USING(id)

— right join with ON-clause
SELECT *
FROM a RIGHT JOIN b
ON a.name = b.authorName

As you can see, a join condition can be written with the keyword ON or the keyword USING. The difference is that the ON keyword is used when each relationship column has a different name and USING when a column with the same name exists in both tables.
Reference table, left and right table?

When we join two tables, there is always a left and a right table (take a look at our syntax examples):

  • The left table is listed on the left side of the OUTER JOIN keywords
  • The right table is listed on the right side of the OUTER JOIN keywords

The outer join which is used decides which table is treated as the reference table. A left join treats the left- and a right join the right table as the reference table. Do you recognize the reference table in the syntax examples? Alright, fasten your seat belts. We’re ready to take off.
Right Outer Joins vs Inner joins

A right outer join is a specialized outer join. Like an inner join, an outer join combines (joins) matching rows that are stored in two different tables. In addition, an outer join also adds unmatched rows from a reference table to the result set. In case of a right outer join this means that when there is a row in the right table which can’t be combined with any row in the left table (according to the join condition), MySQL…

  • takes all selected values from the right table
  • combines them with the column names from the left table.
  • sets the value of every column from the left table to NULL

This is the important difference, because an inner join is not able to select records from a reference table that have no related data in another.

MySQL LEFT JOIN

MySql Left Join

MySql Left Join

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 LEFT JOIN
USING(id)

— left join with ON-clause
SELECT *
FROM a LEFT JOIN b
ON a.name = b.authorName

As you can see, a join condition can be written with the keyword ON or the keyword USING. The difference is that the ON keyword is used when each relationship column has a different name and USING when a column with the same name exists in both tables.

Reference table, left and right table?
When we join two tables, there is always a left and a right table (take a look at syntax examples):

  • The left table is listed on the left side of the OUTER JOIN keywords
  • The right table is listed on the right side of the OUTER JOIN keywords

The outer join which is used decides which table is treated as the reference table. A left join treats the left- and a right join the right table as the reference table. Do you recognize the reference table in the syntax examples?

Left Outer Joins vs Inner joins

A left outer join is a specialized outer join. Like an inner join, an outer join combines (joins) matching rows that are stored in two different tables. In addition, an outer join also adds unmatched rows from a reference table to the result set. In case of a left outer join this means that when there is a row in the left table which can’t be combined with any row in the right table (according to the join condition), MySQL…

  • takes all selected values from the left table
  • combines them with the column names from the right table.
  • sets the value of every column from the right table to NULL

This is the important difference, because an inner join is not able to select records from a reference table that have no related data in another.

MySQL INNER JOIN With comma operator

My Sql Inner JoinHere 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.

Syntax

The following examples are equivalent to the INNER JOIN examples, to make it easy to compare them. The first example builds the Cartesian product of two tables: Every row in the left table is combined with every row in the right table. In ANSI SQL, this is called a cross join. MySQL however doesn’t distinguish between inner joins and cross joins.

— inner join without a condition: a cross join
SELECT *
FROM a, b

– inner join with WHERE-clause
SELECT *
FROM a, b
WHERE a. = b.

When you write inner joins using the comma operator, there is only one way to specify the join condition: with a WHERE-clause.

Basics

An inner join combines all matching rows from two related tables. Two rows match if they are related, for example because they share a common column. When you write an inner join with the comma operator, the join condition which reflects a relationship between tables is added as a WHERE clause. You can use inner joins to SELECT, UPDATE or DELETE data that is stored within MySQL tables. Here are two more examples which are equivalent to the INNER JOIN examples:

SELECT *
FROM tableA a, tableB b
WHERE a.someColumn = b.otherColumn

Further information
You have seen how to write inner joins with the comma operator. From My point of view, it’s “OK” to use this feature. However, using the alternative syntax makes it easier to read your joins. It’s considered as a good behavior to write statements where you can directly see if it’s an INNER JOIN or a CROSS JOIN (the Cartesian product of two tables).

Zend Framework SQL Joins Examples

You may have custom of using advanced queries. It often requires writing complex queries if you are working on enterprise, large scale web application(s).
The use of joins can never be ignored.
Zend Framework developers have done tremendous job by providing simple method for implementing joins.
Lets look some examples of different type of joins.
Before discussing joins lets consider we have two tables, “authors” and “books”.
These are associated with author_id.

1. Inner Join

The simplest query will be
$select = $this->_db->select()
->from(‘books’,array(‘col1’,’col2’…..))
->joinInner(‘authors’,’books.id=authors.bks_id’,array(‘col1’,’col3’…))
->where(‘where condition here’)
->order(‘column name ASC/DESC’);

2. Left Join

$select = $this->_db->select()
->from(‘books’,array(‘col1’,’col2’…..))
->joinLeft(‘authors’,’books.id=authors.bks_id’,array(‘col1’,’col3’…))
->where(‘where condition here’)
->group(‘group by column name here’)
->order(‘column name ASC/DESC’);

3. Right Join

$select = $this->_db->select()
->from(‘books’,array(‘col1’,’col2’…..))
->joinRight(‘authors’,’books.id=authors.bks_id’,array(‘col1’,’col3’…))
->where(‘where condition here’)
->group(‘group by column name here’)
->order(‘column name ASC/DESC’);

4. Full Join

$select = $this->_db->select()
->from(‘books’,array(‘col1’,’col2’…..))
->joinFull(‘authors’,’books.id=authors.bks_id’,array(‘col1’,’col3’…))
->where(‘where condition here’)
->group(‘group by column name here’)
->order(‘column name ASC/DESC’);

5. Cross Join

$select = $this->_db->select()
->from(‘books’,array(‘col1’,’col2’…..))
->joinFull(‘authors’,’books.id=authors.bks_id’,array(‘col1’,’col3’…))
->where(‘where condition here’)
->group(‘group by column name here’)
->order(‘column name ASC/DESC’);

Once you write these queries, you can fetch a single row or multiple rows as