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

时间:2018-09-19 09:32:59

标签: sql postgresql

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_move的{​​{1}}有关系。关系是state

那么我该如何正确编写呢?我尝试过

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

1 个答案:

答案 0 :(得分: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)