3 tables, 1 can be empty

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

3 tables, 1 can be empty

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

lookup how an outer join works..
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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.
Post Reply