在select子句中使用

时间:2014-03-03 01:26:43

标签: sql inner-join windows-server-2003

是否可以引用select语句的结果以在同一查询中返回结果? 我有两个包含以下字段的表

名称n ID, 名称, 电子邮件

关系r ID, 维吾尔, target_id

我想列出原始id和target_id的联系方式(尽管n.id = r.id是主键)。我在想像

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'

我知道这不对,但已经寻找解决方案,并且相信我只是没有使用正确的术语。

1 个答案:

答案 0 :(得分:0)

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'