Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join postgres table on two columns?

Tags:

postgresql

People also ask

How do I join two columns in a table in SQL?

If you'd like to get data stored in tables joined by a compound key that's a primary key in one table and a foreign key in another table, simply use a join condition on multiple columns. In one joined table (in our example, enrollment ), we have a primary key built from two columns ( student_id and course_code ).

How do I join two columns in different tables?

Merging tables by columns. Multiple tables can be merged by columns in SQL using joins. Joins merge two tables based on the specified columns (generally, the primary key of one table and a foreign key of the other).

Does PostgreSQL support full join?

PostgreSQL supports inner join, left join, right join, full outer join, cross join, natural join, and a special kind of join called self-join.

How do I join two tables without common column in PostgreSQL?

Using the “FROM Table1, Table2” Syntax One way to join two tables without a common column is to use an obsolete syntax for joining tables. With this syntax, we simply list the tables that we want to join in the FROM clause then use a WHERE clause to add joining conditions if necessary.


This is possible:

The ON clause is the most general kind of join condition: it takes a Boolean value expression of the same kind as is used in a WHERE clause. A pair of rows from T1 and T2 match if the ON expression evaluates to true for them.

http://www.postgresql.org/docs/9.1/static/queries-table-expressions.html#QUERIES-FROM

Your SQL looks OK.


It's fine. In fact, you can put any condition in the ON clause, even one not related to the key columns or even the the tables at all, eg:

SELECT * from X
LEFT JOIN Y
    ON y.date = x.date
    AND y.code = x.code
    AND EXTRACT (dow from current_date) = 1