2

我有一个 PHP 代码可以从多个表中检索数据,例如:

SELECT RC.customer_id, RC.customer_number, RAA.address_id, RAA.customer_id FROM apps.RA_CUSTOMERS RC, apps.RA_ADDRESSES_ALL RAA WHERE RC.customer_number = 89

但是,由于 customer_id 位于两个单独的表中,我该如何检索它?对于其他一切,在 while() 循环中,我可以做

while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
$row['CUSTOMER_NUMBER']; //this works!
$row['RC.CUSTOMER_ID']; //this does not ...
$row['CUSTOMER_ID']; //this works, but how do I know which table is this from?
}

非常感谢任何帮助!

4

2 回答 2

1

你可以只使用一个ALIAS喜欢RC.customer_id AS cidRAA.customer_id AS raa_cid

SELECT RC.customer_id AS cid, RC.customer_number, RAA.address_id, RAA.customer_id AS raa_cid FROM apps.RA_CUSTOMERS RC, apps.RA_ADDRESSES_ALL RAA WHERE RC.customer_number = 8

现在玩具可以使用

$row['cid']; //the one from RA_CUSTOMERS
$row['raa_cid']; //the one from RA_ADDRESSES_ALL

正如您现在通过放置别名所看到的那样,您可以调用它们并使用给定的别名进行打印

于 2013-06-05T06:15:30.470 回答
1

首先考虑使用JOIN语法。

其次,您始终可以显式地为该列指定别名。

SELECT RC.customer_id rc_customer_id 
       ... 
       RAA.customer_id raa_customer_id
       ...

但在那种特殊情况下,您可能既不需要第二列也不需要别名。如果您打算加入这两个表,它们将是相同的。

SELECT RC.customer_id, 
      ,RC.customer_number 
      ,RAA.address_id 
      -- you probably don't need this column ,RAA.customer_id 
  FROM apps.RA_CUSTOMERS RC JOIN apps.RA_ADDRESSES_ALL RAA 
    ON RC.customer_id = RAA.customer_id
 WHERE RC.customer_number = 89
于 2013-06-05T06:25:14.513 回答