我有一个名为 relOwner 的 mySQL 数据库,它有两列: 所有者 ID、关系所有者
我正在编写一个包含引用数据库的连接的查询:
$query = "SELECT b.Contact, b.ContactB, relOwner.OwnerID, relOwner.RelationshipOwner
FROM b
Left JOIN relOwner
ON b.Contact = relOwner.OwnerID
Left JOIN relOwner
ON b.ContactB = relOwner.OwnerID
";
如何在我的 php 中单独引用 RelationshipOwner 的值?
$RelationshipOwner = $row['RelationshipOwner'];
$RelationshipOwnerB = $row['RelationshipOwner']; <--- Get value from second JOIN
提前致谢。
最佳答案
您似乎在表 b
和表 relOwner
上有两个外键列(即 Contact
和 ContactB
).
根据 Sverri 的评论,您需要为表使用不同的别名(我使用了 ro1
和 ro2
),并从不同的项目中投影不同的名称表格列(例如,在第二个表格列前加上 ro2
):
SELECT b.Contact, b.ContactB, ro1.OwnerID, ro1.RelationshipOwner,
ro2.OwnerID as ro2OwnerId, ro2.RelationshipOwner as ro2RelationshipOwner
FROM b -- Is this table Contact? If so then "Contact b"
Left JOIN relOwner ro1
ON b.Contact = ro1.OwnerID
Left JOIN relOwner ro2
ON b.ContactB = ro2.OwnerID;
然后您可以引用:
$row['ro2RelationshipOwner'];
关于php - 在多个连接中引用同一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27477168/