Jump to content

specify which table variable to use after a JOIN


jeff5656

Recommended Posts

I joined two tables.  However, both tbles have a fieldname called "pod".  How do I echo out that field name so it is from the correct table?

 

$consultsq1 = "SELECT * FROM icu INNER JOIN bundle ON icu.id_incr = bundle.pt_id  "; 
$result = mysql_query ($consultsq1) or die ("Invalid query: " . mysql_error ());
for($n=0;$row = mysql_fetch_assoc ($result);$n++) {
echo $row['pod'];
}

 

When I echo pod, it gives me it from the icu table but not the bundle table.  Thanks!

$consultsq1 = "SELECT *,thecorrecttable.pod FROM icu INNER JOIN bundle ON icu.id_incr = bundle.pt_id  ";
$result = mysql_query ($consultsq1) or die ("Invalid query: " . mysql_error ());
for($n=0;$row = mysql_fetch_assoc ($result);$n++) {
   echo $row['pod'];
}

When joining tables like that you are best to specify what fields you want..

If you want all from 1 table and 2 fields from the second do this.

table1.*, table2.field1, table2.field2

Then you know you are getting the result you want.

 

taquitosensei: that will still select all the fields from all the tables that have been joined.

But I need to query pod from table 1 and pod from table 2, so I need to select BOTH of them (even though they are named the same).  When echoing, how do I echo the correct pod.  Lets say i want to compare the value of pod in table 1 with the value of pod in table 2 (which is what i want to do)?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.