3

我有以下查询

SELECT *
FROM
              ( select distinct
                      r1.rep_code,
                      r1.contact_id,
                      c1.Name,
                      e1.year_num,
                      e1.period_num
                   from
                      entry e1
                         join rep r1 ON e1.rep_code = r1.rep_code
                            join contact c1 on r1.contact_id = c1.contact_id
                   where
                          e1.entry_type = 'SJOB'
                      and e1.age = 0 )

我在第 3 行不断收到错误消息

Token unknown - line 3, char 15
select

顺便说一句,我使用interbase IBConsole,请您给个建议!

4

5 回答 5

3

InterBase 不支持派生表。但是它们对这个查询没有任何好处,所以摆脱它:

              select distinct
                  r1.rep_code,
                  r1.contact_id,
                  c1.Name,
                  e1.year_num,
                  e1.period_num
               from
                  entry e1
                     join rep r1 ON e1.rep_code = r1.rep_code
                        join contact c1 on r1.contact_id = c1.contact_id
               where
                      e1.entry_type = 'SJOB'
                         and e1.age = 0 

...在这种情况下,将为您提供与派生表相同的结果..

于 2011-06-21T15:39:51.780 回答
3

显然,Interbase不支持派生表( SELECT FROM (SELECT))。或者,至少,您正在使用的版本(我不能确定,因为我不使用 Interbase 已经有一段时间了)。此功能是在 Firebird 2.0 中添加的。您在这里有两个选择:

  • 更改您的方法,以便您不使用SELECT FROM (SELECT)(派生表)

    或者

  • 升级到火鸟

如果您对此有自主权,则应该明确选择选项#2。

顺便说一句,Firebird 不要求您为派生表声明别名,尽管如果您将派生表与其他表/派生表联接,这最终将是必要的

于 2011-06-20T14:52:03.260 回答
2

您需要给子查询一个别名。

SELECT *
FROM
          ( select distinct
                  r1.rep_code,
                  r1.contact_id,
                  c1.Name,
                  e1.year_num,
                  e1.period_num
               from
                  entry e1
                     join rep r1 ON e1.rep_code = r1.rep_code
                        join contact c1 on r1.contact_id = c1.contact_id
               where
                      e1.entry_type = 'SJOB'
                  and e1.age = 0 ) AS tbl
于 2011-06-20T08:38:07.577 回答
0

我收到了这个错误:

# 1248 - 每个派生表都必须有自己的别名

尝试类似:

SELECT *
FROM
              ( select distinct
                      r1.rep_code,
                      r1.contact_id,
                      c1.Name,
                      e1.year_num,
                      e1.period_num
                   from
                      entry e1
                         join rep r1 ON e1.rep_code = r1.rep_code
                            join contact c1 on r1.contact_id = c1.contact_id
                   where
                          e1.entry_type = 'SJOB'
                      and e1.age = 0 ) AS entries
于 2011-06-20T08:40:37.483 回答
0
SELECT *
FROM
              ( select distinct
                      r1.rep_code,
                      r1.contact_id,
                      c1.Name,
                      e1.year_num,
                      e1.period_num
                   from
                      entry e1
                         join rep r1 ON e1.rep_code = r1.rep_code
                            join contact c1 on r1.contact_id = c1.contact_id
                   where
                          e1.entry_type = 'SJOB'
                      and e1.age = 0 ) AS TABLE1
于 2011-06-20T08:39:47.800 回答