Jump to content

JOIN SQL Statement Kind of Working


suttercain

Recommended Posts

Hi everyone,

 

I am trying to use the following SQL statement with a triple select list:

 

$year = $_POST['year'];
$disp = $_POST['displacement'];
$mfr = $_POST['engine_mfr'];
$matching = $year . " " . $disp . " " . $mfr;
$statement = "SELECT * FROM device, device_eo, eo, eo_engine_family, engine_family 
            WHERE device.device_id = device_eo.device_id
	    AND device_eo.executive_order = eo.executive_order
	    AND eo.executive_order = eo_engine_family.executive_order
	    AND eo_engine_family.engine_family_name = engine_family.engine_family_name
	    AND engine_family.engine_year ='".mysql_real_escape_string($year)."'
	    AND engine_family.displacement ='".mysql_real_escape_string($disp)."'
	    AND engine_family.engine_mfr ='".mysql_real_escape_string($mfr)."' ".$order."";

 

The above statement does not work, yet if I move the any two of the last three AND lines, it will work... example of when it works...

 

$year = $_POST['year'];
$disp = $_POST['displacement'];
$mfr = $_POST['engine_mfr'];
$matching = $year . " " . $disp . " " . $mfr;
$statement = "SELECT * FROM device, device_eo, eo, eo_engine_family, engine_family 
            WHERE device.device_id = device_eo.device_id
	    AND device_eo.executive_order = eo.executive_order
	    AND eo.executive_order = eo_engine_family.executive_order
	    AND eo_engine_family.engine_family_name = engine_family.engine_family_name
	    AND engine_family.displacement ='".mysql_real_escape_string($disp)."' ".$order."";

 

 

What's the dilly yo?

 

Thanks.

Link to comment
Share on other sites

First, define "not work" -- error, or no results?

 

Second, you'd benefit from proper JOIN syntax using ON clauses; easier on the eye & the parser, since it keeps the join conditions separate from the where clause:

$statement = "SELECT * FROM device
INNER JOIN device_eo ON (  device.device_id = device_eo.device_id )
INNER JOIN eo ON ( device_eo.executive_order = eo.executive_order )
INNER JOIN eo_engine_family ON ( eo.executive_order = eo_engine_family.executive_order )
INNER JOIN engine_family ON ( eo_engine_family.engine_family_name = engine_family.engine_family_name )
WHERE engine_family.engine_year ='".mysql_real_escape_string($year)."'
AND engine_family.displacement ='".mysql_real_escape_string($disp)."'
AND engine_family.engine_mfr ='".mysql_real_escape_string($mfr)."' ".$order."";

 

But can I see the valued of $statement in both cases?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.