从关系中获取价值的正确方法

问题描述 投票:0回答:1
SELECT l.journal_id, %s              
FROM account_move_line AS l
  LEFT JOIN account_journal j ON (j.id = l.journal_id)
WHERE l.state <> 'draft'
AND l.journal_id IN (%s)
AND (l.account_id = j.default_credit_account_id OR
     l.account_id = j.default_debit_account_id)

我收到错误

column l.state does not exist
LINE 17: WHERE l.state <> 'draft'

account_move_line没有"state",但它与account_movestate。关系是move_id

那我怎么写得正确呢?我试过了

WHERE l.move_id.state <> 'draft'

但是我收到了一个错误

missing FROM-clause entry for table "move_id"
LINE 17: WHERE l.move_id.state <> 'draft'
^

基本上我需要与move_id相关的account_move_line state <> 'draft'

sql postgresql
1个回答
0
投票

请使用以下查询:

SELECT l.journal_id, %s              
FROM account_move_line AS l
INNER JOIN account_move am ON l.move_id = am.state_id
  LEFT JOIN account_journal j ON (j.id = l.journal_id)
WHERE am.state <> 'draft'
AND l.journal_id IN (%s)
AND (l.account_id = j.default_credit_account_id OR
     l.account_id = j.default_debit_account_id)
© www.soinside.com 2019 - 2024. All rights reserved.