Page 1 of 1

3 tables, 1 can be empty

Posted: Fri Feb 25, 2005 4:49 am
by rsmarsha
I want a query which will draw from 2 tables and the 3rd if the WHERE clause matches it.

At the moment i have this:

Code: Select all

$show = "SELECT * FROM $table,users WHERE $table.username=users.username AND orderno='$_GETїnumber]'";
$showq = mysql_query($show, $db_conn) or die("query show failed".mysql_error());
$table can be 1 of 2 tables, so ignore that. The 3rd table would be "notes" and does not always have a matching entry. If i use a join like i have above it will not show any results in the case that "notes" has no matching entry.

Any ideas?

Posted: Fri Feb 25, 2005 7:06 am
by timvw
lookup how an outer join works..

Posted: Fri Feb 25, 2005 7:28 am
by rsmarsha
Something like this?

$show = "SELECT * FROM $table LEFT JOIN users ON $table.username=users.username LEFT JOIN notes ON $table.orderno=notes.orderno WHERE $table.orderno='$_GET[number]'";

Seems to be working at the moment. :) Thanks for the tip.