0

次の SQL クエリを使用しています。

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

しかし、私の問題は、両方のテーブルに同じ名前のフィールドがあることplaceです. では、PHP コードで名前placeの付いた列を選択するにはどうすればよいでしょうか? table2次の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 に答える