jeff5656 Posted January 20, 2010 Share Posted January 20, 2010 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! Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted January 20, 2010 Share Posted January 20, 2010 $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']; } Quote Link to comment Share on other sites More sharing options...
Buddski Posted January 20, 2010 Share Posted January 20, 2010 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. Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted January 20, 2010 Author Share Posted January 20, 2010 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)? Quote Link to comment Share on other sites More sharing options...
Buddski Posted January 20, 2010 Share Posted January 20, 2010 SELECT table1.pod as `pod1`, table2.pod as `pod2` FROM .... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.