Mahi
Mahi

Reputation: 190

How to user SQL two table field.? PHP

Here I want to access two table field but I cant get success. Here is my little code. please check that. I want to access Analysis.Right and tabl3.right. I am printing its with foreach loop. like $res['Right'] of Analysis and $res['right'] of tabl3. when I try to print this it's show me error

Undefind index Right.

any one can help me

$qry = "select Analysis.Q_Id, tabl3.Q_Id, Analysis.Right, tabl3.right  from tabl3 INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";

please help..

Upvotes: 0

Views: 36

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133360

you have tow column with right name related to different table so there is not a column name right but 'Analysis.Right ' or 'tabl3.right'

or you can assign an alias for set the column name equalt to Right where you need .. eg:

  $qry = "select 
                Analysis.Q_Id
              , tabl3.Q_Id
              , Analysis.Right as Right
              , tabl3.right  as Right_t3
          from tabl3 
          INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269643

Your result set has columns with the same name. Give them different names:

select t3.Q_Id, a.Right as a_right, t3.right as t3_right
from tabl3  t3 inner join
      Analysis a
      on a.Q_Id = t3.Q_Id
where a.Q_Id = 3;

When you look for the names in your code, look for a_right and t3_right.

Note that you don't need to return Q_Id twice. The ON clause guarantees that the values are the same.

Upvotes: 1

Related Questions