【发布时间】:2019-06-27 20:27:18
【问题描述】:
我有一张桌子:
| acctg_cath_id | parent | description |
| 1 | 20 | Bills |
| 9 | 20 | Invoices |
| 20 | | Expenses |
| 88 | 30 |
| 89 | 30 |
| 30 | |
我想创建一个自我加入,以便将我的项目分组到一个父项下。
试过了,还是不行:
SELECT
accounting.categories.acctg_cath_id,
accounting.categories.parent
FROM accounting.categories a1, accounting.categories a2
WHERE a1.acctg_cath_id=a2.parent
我收到错误:invalid reference to FROM-clause entry for table "categories"
当我尝试时:
a.accounting.categories.acctg_cath_id
b.accounting.categories.acctg_cath_id
我收到错误:cross-database references are not implemented: a.accounting.categories.acctg_cath_id
期望的输出:
- 费用(父级 20)
- 账单(儿童 1)
- 发票(儿童 9)
我在这里做错了什么?
【问题讨论】:
-
postgresql self join的可能重复
-
@emix 好吧,我尝试了 a.accounting.categories.acctg_cath_id 和 b.accounting.categories.acctg_cath_id。但这对我不起作用。
-
你得到了什么输出,你想得到什么?
-
@jmelesky 我收到一个错误。我想将
acctg_cath_id分组到parentid 下。错误invalid reference to FROM-clause entry for table "categories" -
啊,我明白了。在您的原始查询中,您希望选择
a1.acctg_cath_id, a1.parent而不是accounting.categories.acctg_cath_id- 只是命名表是不明确的,因为您在连接的两个不同部分处理它。
标签: sql postgresql