SQL:选择时出现ORA-00933错误

时间:2012-01-11 10:13:26

标签: sql oracle

我正在尝试从3个表中执行一些复杂的选择,所有表都由b_id加入:

select max(bs.b_id), 
       h.b_type_id, 
       t.name_id 
  from b_state as bs, 
       t_info as t, 
       history as h 
 where bs.b_id = t.b_id 
   and bs.b_id = h.b_id 
   and t.name_id in (???) 
   and bs.is_detached = ? 
 group by h.b_type_id, 
          t.name_id

在MySQL中,它提供了我想要的内容,但它似乎在PSQL和Oracle中失败,加上在Oracle中的执行导致:“ORA-00933: SQL command not properly ended”(这通常发生在Google建议的INSERT查询中)。

我认为有一种DB独立的方式可以做同样的事情。请指教。

4 个答案:

答案 0 :(得分:2)

尝试:

select max(bs.b_id), h.b_type_id, t.name_id 
  from b_state bs
  inner join t_info t on bs.b_id = t.b_id
  inner join history h on bs.b_id = h.b_id
  where t.name_id in (???) and bs.is_detached = ?
  group by h.b_type_id, t.name_id

答案 1 :(得分:1)

试试这个:

select max(bs.b_id),
       h.b_type_id,
       t.name_id
  from b_state bs,
       t_info t,
       history h
 where bs.b_id = t.b_id
   and bs.b_id = h.b_id
   and t.name_id in (???)
   and bs.is_detached = ?
 group by h.b_type_id,
          t.name_id 

表格中的“as”使您在Oracle中出现问题。

编辑:我使用了SQL-86语法,因为这是你最初发布的,但你应该在默认情况下使用SQL-92语法。

答案 2 :(得分:1)

Oracle使用:varname绑定变量而不是?

所以尝试像

这样的东西
select 
  max(bs.b_id),
  h.b_type_id,
  t.name_id    
from 
  b_state as bs,
  t_info  as t,
  history as h  
where 
  bs.b_id = t.b_id            and 
  bs.b_id = h.b_id            and 
  t.name_id in (:1, :2, :3)   and 
  bs.is_detached = :4
group by 
 h.b_type_id,           
 t.name_id 

答案 3 :(得分:1)

我建议给最大化别名和(如果需要)逗号分隔in子句中的参数,如下所示:

select max(bs.b_id) max_b_id, 
       h.b_type_id, 
       t.name_id 
  from b_state as bs, 
       t_info as t, 
       history as h 
 where bs.b_id = t.b_id 
   and bs.b_id = h.b_id 
   and t.name_id in (?,?,?) 
   and bs.is_detached = ? 
 group by h.b_type_id, 
          t.name_id