根据左联接子查询ID值选择基础表ID

时间:2018-08-29 17:38:43

标签: sql postgresql

考虑我应该选择我在d.id中的a.id

 SELECT 
  a.id,
  a.value_entered_date,
  d.name,
  d.order_date
  d.zip
FROM table a
LEFT JOIN
 (
SELECT 
b.id,b.name,c.value,b.order_date
FROM table b
LEFT join table c
on b.id=c.id
) as d
WHERE a.id in (d.id)
and a.value = d.value 

在这里我应该选择d.id中的a.id

2 个答案:

答案 0 :(得分:0)

使用左连接

SELECT 
      a.id,
      a.value_entered_date,
      d.name,
      d.order_date
      d.zip
    FROM table a
    where a.id in ( select d.id from 
     (
    SELECT 
    b.id,b.name,c.value,b.order_date
    FROM table b
    LEFT join table c
    on b.id=c.id
    ) as d
 )

答案 1 :(得分:0)

我推测您只需要一个join条件。看起来像:

SELECT a.id, a.value_entered_date,
       d.name, d.order_date d.zip
FROM table a LEFT JOIN
     (SELECT b.id, b.name, c.value, b.order_date
      FROM table b LEFT join
           table c
           ON b.id = c.id
     ) d
     ON a.id = d.id AND a.value = d.value ;

我很确定这实际上可以简化为:

SELECT a.id, a.value_entered_date,
       b.name, b.order_date, c.zip
FROM table a LEFT JOIN
     table b
     ON a.id = b.id LEFT JOIN
     table c
     ON a.id = c.id AND a.value = c.value