帮助SQL查询 - 使用INTERBASE进行子查询

时间:2011-06-20 08:35:38

标签: sql select firebird interbase

我有以下查询

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 !!

5 个答案:

答案 0 :(得分:3)

显然,Interbase does not support derived tablesSELECT FROM (SELECT))。或者,至少,你正在使用的版本(我不能确定,因为我不使用Interbase已经有一段时间了)。 Firebird 2.0中添加了此功能。你有两个选择:

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

  • 升级到Firebird

如果你有自主权,你应该明确地选择#2选项。

BTW,Firebird不要求你为派生表声明别名,尽管如果你将派生表与其他表/派生表联系起来,那将最终是必要的

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

答案 2 :(得分:1)

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 

...在这种情况下会给你与派生表相同的结果..

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

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