根据postgresql中的条件左连接

问题描述 投票:0回答:4

当我执行下面的查询时,它会抛出错误说:语法错误在“as”或附近。

我们如何解决这个问题?

还有其他方法吗?

(select * from my_table1 where status='6') as a
left JOIN 
(select * from my_table1 where status='1') as b
ON (a.application_id = b.application_id)
sql postgresql
4个回答
1
投票

通过使用子查询中的选择尝试如下所示

select a.* from (select * from my_table1 where status='6') as a
left JOIN 
(select * from my_table1 where status='1') as b
ON a.application_id = b.application_id

1
投票

你错过了最初的select * from条款。而且,不需要子查询;你只需要小心条件放置。

select *  
from my_table1 a 
left join my_table1 b on b.status='1' and
                         b.application_id = a.application_id
where a.status='6'

0
投票

您已从子查询中选择所有值以加入它们:

select a.* from (select * from my_table1 where status='6') as a
left JOIN 
select b.* from (select * from my_table1 where status='1') as b
ON (a.application_id = b.application_id)

0
投票
select * from my_table1 as A 
LEFT JOIN  my_table1 as B
ON (A.application_id = A.application_id)
WHERE A.status='6' AND B.status='1'
© www.soinside.com 2019 - 2024. All rights reserved.