0

Firebird Db 将图表帐户记录存储在表中:

CREATE TABLE CHARTACC 
 (
  ACCNTNUM       Char(8) NOT NULL, -- Account ID (Primary Key)
  ACCPARNT       Char(8),          -- Parent ID
  ACCCOUNT       Integer,          -- account count
  ACCORDER       Integer,          -- order of children in nodes  
  ACCTITLE       varchar(150),     
  ACDESCRP       varchar(4000),
  DTCREATE       timestamp         -- date and time of creation
  )

我必须编写查询,它只从表中选择最后一个节点,例如没有子节点的节点(child2、child3、subchild1、subchild2、subchild3 和 subchild4)。

在此处输入图像描述

4

2 回答 2

3

Jerry 建议的not in方法通常在 Interbase/Firebird/Yaffil/RedDatabase 系列中运行缓慢,不使用索引等。

另一种可能的表示Select X from T1 where NOT EXISTS ( select * from t2 where t2.a = t1.b)也是如此 - 它也可能变得非常慢。

我同意这些查询更好地代表了人类想要的东西,因此更具可读性,但仍然不建议在 Firebird 上使用它们。我在 1990 年代在做类似 Herbalife 的应用程序时被严重咬伤,我选择了这种类型的请求包裹在一个循环中进行每月自下而上的统计- 并且每次迭代在 Interbase 5.5 中update ... where not exists ...缩放为o(n^2) 。诚然,从那时起,Firebird 3 取得了长足的进步,但仍然不推荐这种“直接”方法。

更传统的 SQL 和 FB 友好的方式来表达它,尽管不那么直接且更难阅读,将是Select t1.x from t1 LEFT JOIN t2 on t1.a=t2.b WHERE t2.y IS NULL

于 2018-12-12T08:22:01.753 回答
1

您的查询需要像这样工作:

select * from CHARTACC where ACCNTNUM not in (select ACCPARNT from CHARTACC)

简而言之,从该表中选择在其父字段的同一表中找不到其标识符的项目。

于 2018-12-12T02:21:38.493 回答