Thread: Select clause in JOIN statement
<div dir="ltr">Is it valid to specify a SELECT statement as part of a JOIN clause?<div style="style"><br /></div><div style="style">Forexample:</div><div style="style"><br /></div><div style="style">SELECT table1.f1, table1.f2 FROM table1</div><divstyle="style">INNER JOIN </div><div style="style">(SELECT table2.f1, table2.f2 FROM table2) table_aux ONtable1.f1 = table_aux.f1</div><div style="style"><br /></div><div style="style">Respectfully,<br /></div><div style="style">JorgeMaldonado</div></div>
It works. Also consider views. Just used this on a my db: SELECT * FROM tblcus_customer INNER JOIN ( SELECT * FROM tblcus_customer_status WHERE status_id > 0) AS b ON tblcus_customer.status = b.status_id You can even join with a function result. Regards, Luca. 2013/6/14 JORGE MALDONADO <jorgemal1960@gmail.com>: > Is it valid to specify a SELECT statement as part of a JOIN clause? > > For example: > > SELECT table1.f1, table1.f2 FROM table1 > INNER JOIN > (SELECT table2.f1, table2.f2 FROM table2) table_aux ON table1.f1 = > table_aux.f1 > > Respectfully, > Jorge Maldonado
<div>På fredag 14. juni 2013 kl. 01:10:51, skrev Luca Vernini <<a href="mailto:lucazeo@gmail.com" target="_blank">lucazeo@gmail.com</a>>:</div><blockquotestyle="border-left: 1px solid rgb(204, 204, 204); margin: 0pt0pt 0pt 0.8ex; padding-left: 1ex;"><div style="display:inline; font-family: monospace; font-size: 12px;">It works.<br/> Also consider views.<br /><br /> Just used this on a my db:<br /><br /> SELECT * FROM tblcus_customer<br /> INNERJOIN<br /> ( SELECT * FROM tblcus_customer_status WHERE status_id > 0) AS b<br /> ON tblcus_customer.status = b.status_id</div></blockquote><div> </div><div>Thisquery is the same as a normal JOIN:</div><div><style type="text/css"></style><preclass="western" style="background: #ffffff; border: none; padding: 0in"> <font color="#000080"><font face="DejaVu Sans Mono"><b>SELECT</b> </font></font><font color="#000000"><font face="DejaVuSans Mono">*</font></font> <font face="DejaVu Sans Mono"><font color="#000080"><b>FROM </b></font><font color="#000000">tblcus_customer</font></font> <font color="#000000"> </font><font color="#000080"><font face="DejaVu Sans Mono"><b>INNER JOIN</b></font></font> <font color="#000080"> </font><font color="#000000"><font face="DejaVu Sans Mono">tblcus_customer_status b</font></font> <font color="#000000"> </font><font color="#000080"><font face="DejaVu Sans Mono"><b>ON </b></font></font><font color="#000000"><fontface="DejaVu Sans Mono">tblcus_customer.status = b.status_id </font></font><font color="#000080"><fontface="DejaVu Sans Mono"><b>AND </b></font></font><font color="#000000"><font face="DejaVu Sans Mono">b.status_id> </font></font><font color="#0000ff"><font face="DejaVu Sans Mono">0</font></font> </pre> or</div><div><style type="text/css"></style><pre class="western" style="background: #ffffff; border: none; padding:0in"> <font color="#000080"><font face="DejaVu Sans Mono"><b>SELECT</b></font> </font><font color="#000000"><font face="DejaVuSans Mono">*</font></font> <font face="DejaVu Sans Mono"><font color="#000080"><b>FROM </b></font><font color="#000000">tblcus_customer</font></font> <font color="#000000"> </font><font color="#000080"><font face="DejaVu Sans Mono"><b>INNER JOIN</b></font></font> <font color="#000080"> </font><font color="#000000"><font face="DejaVu Sans Mono">tblcus_customer_status b</font></font> <font color="#000000"> </font><font color="#000080"><font face="DejaVu Sans Mono"><b>ON </b></font></font><font color="#000000"><fontface="DejaVu Sans Mono">tblcus_customer.status = b.status_id</font></font> <font face="DejaVu Sans Mono"><font color="#000080"><b>WHERE </b></font><font color="#000000">b.status_id > </font><fontcolor="#0000ff">0</font></font></pre> But you can JOIN on SELECTs selecting arbitrary stuff.</div><div> </div><divclass="origo-email-signature">--<br /> Andreas Joseph Krogh <andreak@officenet.no> mob: +47 909 56 963<br /> Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no<br /> Public key: http://home.officenet.no/~andreak/public_key.asc</div><div> </div>