Question

Is it possible to reference results from the select statement to return results in same query? I have two tables that contain the following fields

Name n id, name, email

Relationship r id, Realtion, target_id

I would like to list contact details of both original id and those of target_id (though n.id = r.id is the primary key). I was thinking something like

Select
n.id as 'Seller_ID',
n.name as 'Seller_name',
n.email as 'Seller_Email',
r.realtion,
r.target_id as 'Buyer_ID',
          (select n.name as 'Buyer_name',
                  n.email as 'Buyer_email;

           from name n
                inner join relationship r on n.id =r.target_id)

 from name n
      inner join relationship r on n.id = r.id

 where n.id = 'ABC123'

I know this is not right but have searched for solutions and believe I am just not using the correct terminology.

Was it helpful?

Solution

Select
n.id as 'Seller_ID',
n.name as 'Seller_name',
n.email as 'Seller_Email',
r.relation,
t.id as 'Buyer_ID',
t.name as 'Buyer_name',
t.email as 'Buyer_email'
from `name` AS n
      inner join relationship r on n.id = r.id
      inner join name t on r.target_id = t.id
 where n.id = 'ABC123'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top