8.9. Sophisticated Joins

Joins are the heart of every complex query. Whenever more than just one table is involved in a query, joins must be performed. So far you have dealt with normal joins, but in this section you will take a closer look at some more helpful and useful features.

8.9.1. Left, Right, and Co.

In general performing simple joins is easy—in some cases, however, it is necessary to perform some more complex operations. In this section you will start to deal with a set of different join operations such as left and right joins.

Let's start with an example. Two tables must be created:

phpbook=# CREATE TABLE event (id int4, name text);
CREATE
phpbook=# CREATE TABLE comment (event_id int4, comment text);
CREATE

The first table stores ...

Get PHP and PostgreSQL: Advanced Web Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.