.ig >>
<STYLE TYPE="text/css">
<!--
        A:link{text-decoration:none}
        A:visited{text-decoration:none}
        A:active{text-decoration:none}
        OL,UL,P,BODY,TD,TR,TH,FORM { font-family: arial,helvetica,sans-serif;; font-size:small; color: #333333; }

        H1 { font-size: x-large; font-family: arial,helvetica,sans-serif; }
        H2 { font-size: large; font-family: arial,helvetica,sans-serif; }
        H3 { font-size: medium; font-family: arial,helvetica,sans-serif; }
        H4 { font-size: small; font-family: arial,helvetica,sans-serif; }
-->
</STYLE>
<title>shsql: join</title>
<body bgcolor=99cc99 vlink=0000FF>
<br>
<br>
<center>
<table cellpadding=2 bgcolor=FFFFFF width=550 ><tr>
<td align=right><a href="shsql_home.html">
<img src="img/shsql.gif" border=0><br><small>SQL database system</a> &nbsp; </td></tr>
<td>
.>>

.TH join TDH "18-MAR-2004   TDH scg@jax.org" 

.SH JOIN
The \fCJOIN\fR keyword, which is used as part of a 
.ig >>
<a href="select.html">
.>>
\0SELECT command,
.ig >>
</a>
.>>
allows a relational join to be performed on two or three tables.
The \fCJOIN .. ON\fR syntax \fBmust\fR be used; alternatives aren't supported.  
Usage:
.LP
\fCSELECT .. 
.br
FROM \fIleft_table\fC [AS|= \fIalias1\fC] [\fIjointype\fR] JOIN \fIright_table\fC [AS|= \fIalias2\fC]
.br
\fC[ [\fIjointype2\fC] JOIN \fItable3\fR [AS|= \fIalias3\fC]
.br
\fCON \fIbinding\fR
.br
\fCWHERE ..\fR (etc)

.ig >>
<br><br><br>
.>>

.SH Examples
.nf
SELECT c.name, t.description
FROM customers (AS c) JOIN transactions (AS t) 
ON c.cust_id = t.cust_id

select p.*, ps.currentstatus
from patients = p left join patientstatus = ps
on p.patientid = ps.patientid
where ps.class = "B"
order by p.patientid

SELECT * FROM people JOIN pi_ref JOIN inst (as i)
ON people.id = pi_ref.people_id AND pi_ref.inst_id = i.id
ORDER BY people.lastname

.SH Usage notes
.LP
When using \fCJOIN\fR, fieldnames must be specified using \fItable\fR.\fIfieldname\fR
notation everywhere in the SQL command.
Table name aliases may be defined using \fCAS\fR or \fC=\fR (as in the above examples); this 
helps to maintain brevity. 
.LP
\fCON \fIbinding\fR specifies the key field(s) that will be matched up when performing 
the join.  The only supported comparison operation is equality (=), and matching is case-insensitive.
Multiple bindings may be connected using \fCAND\fR.
.LP
.ig >>
<table cellpadding=0 cellspacing=4>
.>>
.ig >>
</td></tr><tr bgcolor=F0F0F0><td>
.>>
Join type
.ig >>
</td><td>
.>>
Method
.ig >>
</td><td>
.>>
Multiple rows having same key*

.ig >>
</td></tr><tr bgcolor=F0F0F0><td>
.>>
\fBINNER\fR 
.ig >>
</td><td>
.>>
This is the default.  Content must be present on both sides to produce an output record.  
.ig >>
</td><td>
.>>
Right side only

.ig >>
</td></tr><tr bgcolor=F0F0F0><td>
.>>
\fBLEFT\fR
.ig >>
</td><td>
.>>
All left side rows kept; right side rows joined when available, filled in
with NULLs otherwise.
.ig >>
</td><td>
.>>
Right side only

.ig >>
</td></tr><tr bgcolor=F0F0F0><td>
.>>
\fBRIGHT\fR
.ig >>
</td><td>
.>>
All right side rows kept; left side rows joined when available, filled in with NULLs otherwise.
.ig >>
</td><td>
.>>
Right side only

.ig >>
</td></tr><tr bgcolor=F0F0F0><td>
.>>
\fBOUTER\fR
.ig >>
</td><td>
.>>
All left side and right side rows kept; companion content joined when available, filled in 
with NULLs otherwise.
.ig >>
</td><td>
.>>
Right side only

.ig >>
</td></tr><tr bgcolor=F0F0F0><td>
.>>
\fBINNERDL\fR 
.ig >>
</td><td>
.>>
Same as INNER.
.ig >>
</td><td>
.>>
Left side only

.ig >>
</td></tr><tr bgcolor=F0F0F0><td>
.>>
\fBLEFTDL\fR
.ig >>
</td><td>
.>>
Same as LEFT.
.ig >>
</td><td>
.>>
Left side only

.ig >>
</td></tr><tr bgcolor=F0F0F0><td>
.>>
\fBRIGHTDL\fR
.ig >>
</td><td>
.>>
Same as RIGHT.
.ig >>
</td><td>
.>>
Left side only

.ig >>
</td></tr><tr bgcolor=F0F0F0><td>
.>>
\fBOUTERDL\fR
.ig >>
</td><td>
.>>
Same as OUTER.
.ig >>
</td><td>
.>>
Left side only

.ig >>
</table>
.>>

.LP
* Multiple rows having same key may be present on the left side or the right
side.  Multiple rows will be preserved in the join result
if the appropriate \fIjointype\fR is used.  Multiple rows having same key are
never permitted on both left and right sides.  The \fIjointypes\fR ending in
\fBDL\fR are non-standard; \fBDL\fR stands for "duplicates in left side".

.LP
Three-table joins are supported (see the 3rd example above).
The 2nd and 3rd tables are joined first, then that result is joined with the first
table.  Each of these join operations has a left side and a right side, and
its own \fIjointype\fR.

.LP
Naming of the resultant fields is described in the
.ig >>
<a href="select.html#resultnames">
.>>
\0SELECT manual page.
.ig >>
</a>
.>>
.LP
\fBshsql\fR performs joins by executing a separate program called \fCshsql_join\fR.
\fCshsql_join\fR must be in the current command PATH, or 
in the \fCdbbin\fR directory as defined in your 
.ig >>
<a href="config.html#dbbin">
.>>
\0project config file.
.ig >>
</a>
.>>

.ig >>
<br>
<br>
</td></tr>
<td align=right>
<a href="shsql_home.html">
<img src="img/shsql.gif" border=0></a><br>
<a href="Copyright.html">Copyright Steve Grubb</a> &nbsp;
</td></tr>
</table>
.>>
