Referring to the same line in multiple joins

I have mySQL db named relOwner which has two columns: OwnerID, RelationshipOwner

I am writing a join query referencing db:

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

How can I reference RelationshipOwner values โ€‹โ€‹separately in my php?

$RelationshipOwner = $row['RelationshipOwner'];
$RelationshipOwnerB = $row['RelationshipOwner']; <--- Get value from second JOIN

Thanks in advance.

+4
source share
1 answer

It seems that you have two foreign key columns in the table bfor the table relOwner(viz Contactand 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'];
+3

All Articles