fetching from join

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
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

fetching from join

Post by kpraman »

Hello,

I am trying to fetch values from two tables with the help of join statements. if the two tables are having same field name(which i am not going to use in joining). how to differeciate them and get their values.

I am getting the wrong values.


Code: Select all

$query=mysql_query("SELECT * FROM tbl1, tbl2 WHERE tbl1.id=tbl2.id");
   while($r=mysql_fetch_array($query))
     {
          $fetch[]=$r;
     }
tbl1 and tbl2 contains field name enableFlag.

i want to get tbl1 enableFlag value and also tbl2 enableFlag. How to do this?

Thanx
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

The best way (in my opinion) is to alias the column names.

Code: Select all

$query=mysql_query("SELECT *, tbl1.enableFlag as tbl1_enableFlag, tbl2.enableFlag as tbl2_enableFlag FROM tbl1, tbl2 WHERE tbl1.id=tbl2.id");
   while($r=mysql_fetch_array($query))
     {
          $fetch[]=$r;
     }
You'll then have a couple of fields called tbl1_enableFlag and tbl2_enableFlag respectively that contain the data from each table.
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

Thanx.
Post Reply