MySQL连接表并获取具有相同键名的值

时间:2016-01-06 01:39:37

标签: php mysql join

我正在尝试从一个表中选择所有地址ID并加入其他表中的属性。我需要通过entity_id连接表A和C的值,但是因为表具有相同的键"值"第二个连接正在替换第一个连接的值。

我在这里做错了什么?

    $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);

但是我得到了一个4的数组.A.value被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)

1 个答案:

答案 0 :(得分:2)

也许您可以为一列添加别名以赋予其唯一名称?即。

$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";
...

编辑:可能有其他方法可以做到这一点,但是看一些示例数据和可能的一些代码来处理结果会对我有所帮助。