I have these following tables:
suppliers
id | name
-----------
1 | sample
addresses
id | address | owner_type | owner_id
-------------------------------------
1 | adres | Supplier | 1
phone_numbers
id | number | owner_type | owner_id | phone_type
------------------------------------------------
1 | 12345 | Supplier | 1 | phone
2 | 67890 | Supplier | 1 | fax
It seem not a problem for me for addresses table, I use this query, I tested it and it work fine.
Select suppliers.*, addresses.address AS address FROM suppliers LEFT JOIN addresses ON (addresses.owner_id = suppliers.id AND addresses.owner_type = 'Supplier')
Now the problem is in phone_number
table, there is 2 record with 2 different conditional, as you can see:
id | number | owner_type | owner_id | phone_type
------------------------------------------------
1 | 12345 | Supplier | 1 | phone
2 | 67890 | Supplier | 1 | fax
There is phone_type
field -> phone
and fax
, and I want to show both of that in result, like this:
id | name | address | phone | fax
---------------------------------
1 | sample | adres | 12345 | 67890
I try this query:
LEFT JOIN phone_numbers ON (phone_numbers.owner_id = suppliers.id AND phone_numbers.owner_type = 'Supplier' AND phone_type = 'phone')
Unfortunately that query just for 1 condition (phone
) , how I get the fax value?
I use PostgreSQL.