2

I have a PHP code to retrieve data from multiple tables eg:

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

However, since customer_id is in two separate tables, how do I retrieve it? For everything else, in the while() loop, I can just do

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?
}

Any help greatly appreciated!

2 Answers 2

1

You can just use an ALIAS like RC.customer_id AS cid and RAA.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

Now toy can call it using

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

As you see by placing an alias now you call them and print by using alias given

Sign up to request clarification or add additional context in comments.

Comments

1

First of all consider using JOIN syntax.

Secondly you can always explicitly give an alias to the column.

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

But in that particular case you probably don't need neither second column nor an alias. If you meant to join those two tables they will be the same.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.