简体   繁体   中英

MySQL join tables and get values that have the same key name

I am trying to select all of the address id's from one table and join the attributes from other tables. I need to join values on tables A and C by entity_id however since the tables have the same key "value" the second join is replacing the value of the first.

What am I doing wrong here?

    $query = "SELECT 
        A.parent_id,
        B.value,
        C.value
    FROM customer_address_entity AS A
    LEFT JOIN customer_address_entity_varchar AS B 
        ON A.entity_id = B.entity_id
    LEFT JOIN customer_address_entity_text AS C 
        ON A.entity_id = C.entity_id
    LIMIT 100";

    var_dump($query);

But I get an array of 4. A.value is being overwritten by C.value

array (size=4)
  'parent_id' => string '7' (length=1)
  'entity_id' => string '6' (length=1)
  'value' => string '19 STANDIFORD CT' (length=16)
  'attribute_id' => string '25' (length=2)

Perhaps you could alias one column to give it a unique name? ie

$query = "SELECT 
    A.parent_id,
    B.value,
    C.value as Foobar
FROM customer_address_entity AS A
LEFT JOIN customer_address_entity_varchar AS B 
    ON A.entity_id = B.entity_id
LEFT JOIN customer_address_entity_text AS C 
    ON A.entity_id = C.entity_id
LIMIT 100";
...

EDIT: There are likely other ways of doing this, but it would be helpful for me to see some example data and possibly some code for the processing of results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM