Exn
Exn

Reputation: 809

Data from different tables have the same name, how to retrieve data?

Here is my code:

$sql = "SELECT table1.Insert_ID, table1.Type, table1.Date "
    . "table2.User, table2.Date "
    . "FROM Insert AS table1 "
    . "INNER JOIN Contact AS table2 ON table2.Contact_ID = table1.Contact_ID";

$x =0;

while($row = odbc_fetch_array($res)){

$x++;

 $values= ($x . ": " . " Insert ID:". $row['Insert ID'] . " Date Created: " . $row['Date'] . " Date Modified:" . "\n");

print($values);


}

I want Date from both table1 and table2, how do I proceed to retrieve the data if they both have the same name?

Upvotes: 0

Views: 44

Answers (2)

xQbert
xQbert

Reputation: 35333

Column aliases.

$sql = "SELECT table1.Insert_ID, table1.Type, table1.Date as T1Date "
    . "table2.User, table2.Date as T2Date"
    . "FROM Insert AS table1 "
    . "INNER JOIN Contact AS table2 ON table2.Contact_ID = table1.Contact_ID";

Upvotes: 1

Vulcronos
Vulcronos

Reputation: 3456

Use an alias.

SELECT table1.Insert_ID, table1.Type, table1.Date as Date1 "
. "table2.User, table2.Date as Date2 "
. "FROM Insert AS table1 "
. "INNER JOIN Contact AS table2 ON table2.Contact_ID = table1.Contact_ID";

Upvotes: 1

Related Questions