Solved

Referencing the same column from table aliases

Posted on 2004-03-31
1
180 Views
Last Modified: 2008-03-06
These tables don't make a lot of sense, but they are for illustrative purposes only:

FactTable:
ID | Result
1     Dead
2     Alive
3     Open

RefTable
Name | FactID1 | FactID2
Jane     1             2
Bob      1             3

$query = "select Name, Res1.Result, Res2.Result from RefTable, FactTable Res1, FactTable Res2
               where RefTable.Fact1ID = Res1.ID and RefTable.Fact2ID = Res2.ID"

$res = mssql_query($query);
$n = mssql_num_rows($res);
if ($n)
{
   $a = mssql_fetch_array($res);
   $name = $a['Name'];
   // MY QUESTION: how do I reference Res1.Result and Res2.Result here? $a['Result'] won't work,
   // it's ambigous. Also fully qualified $a['Res1.Result'] does not work. What is the correct syntax?
}

Thanks!
0
Comment
Question by:RichardFox
1 Comment
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 125 total points
ID: 10726758
> $query = "select Name, Res1.Result, Res2.Result from RefTable, FactTable
> Res1, FactTable Res2
>                where RefTable.Fact1ID = Res1.ID and RefTable.Fact2ID = Res2.ID"

query can be changed to...

$query = "select Name, Res1.Result as Result1, Res2.Result as Result2 from RefTable, FactTable Res1, FactTable Res2
               where RefTable.Fact1ID = Res1.ID and RefTable.Fact2ID = Res2.ID"


.
.
$val = $a['Result1']

Cheers!
venkat.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question