0

我正在使用以下 SQL 查询:

Select * from table1 as t1, table2 as t2 where t1.id = t2.col

但我的问题是这两个表都有同名的字段,place. 那么如何placetable2我的 PHP 代码中选择带有名称的列呢?我想使用以下 php 代码

 while($row_records = mysql_fetch_array($result_records))
    {

            <?  echo $row_records['place']; ?>

     }

如何从特定表中获取字段?

4

2 回答 2

6

从不使用...

Select * from ...

...在生产环境中 - 始终明确指定要返回的列。

因此,您可以将 SQL 修改为:

Select t1.Place as T1Place, t2.Place as T2Place
  from table1 as t1, table2 as t2 where t1.id = t2.col

因此,在您的 PHP 中,您将拥有:

 while($row_records = mysql_fetch_array($result_records))
 {

        <?  echo $row_records['T2Place']; ?>

 }
于 2009-09-23T10:21:53.850 回答
3

为什么不使用表别名和字段名。例如,

    Select t1.place as t1_place, t2.place as t2_place 
      from table1 as t1, table2 as t2 where t1.id = t2.col

在您的 PHP 代码中,您可以使用

while($row_records = mysql_fetch_array($result_records))
    {
    echo $row_records['t1_place']; 
    echo '<br />';
    echo $row_records['t2_place']; 
    }
于 2009-09-23T10:20:17.097 回答