缺少FROM子句错误

时间:2013-11-25 12:17:40

标签: sql postgresql select

以下相当复杂的查询会生成错误:

  

错误:表“裁判”缺少FROM子句条目第14行:...从裁判员中选择people.id,其中people.id = referee.id ...

select people.id, lower(lastname) as lname from people 
where (interests ~* '$key' or biography ~* '$key'
or exists (select * from authors, articles where 
articles.keywords ~* '$key' and articles.id=authors.article_id
and authors.author_id=people.id)) and not inactive and email is not null
and people.id not in (
select people.id from people, authors where authors.author_id = people.id 
and authors.article_id = $article_id)
and people.id not in (
select people.id from articles, referees where 
people.id = referee_id and refused is null and reported is null and 
editors_decision is null and article_id = articles.id)
and people.id not in (
select people.id from referees where people.id = referee.id and reported is not null
and date_trunc('day', current_timestamp - cast(reported as timestamp)) > 30)

如果最后一个选择子句(即从and people.id not in (timestamp)) > 30))被删除并且错误消息指向该子句,则错误消失,因此必须是问题所在。但是select子句确实有一个FROM,而FROM指的是据称丢失的裁判表。此外,select子句的结构与上面的结构相同。

我想我错过了一些明显的东西,但我看不清楚是什么。有什么想法吗?

[这是使用Postgresql 9.0.3并且正在通过PHP执行,因此在上面的SQL中以$开头的变量引用]

2 个答案:

答案 0 :(得分:0)

select people.id, lower(lastname) as lname from people 
where (interests ~* '$key' or biography ~* '$key'
or exists (select * from authors, articles where 
articles.keywords ~* '$key' and articles.id=authors.article_id
and authors.author_id=people.id)) and not inactive and email is not null
and people.id not in (
select people.id from people, authors where authors.author_id = people.id 
and authors.article_id = $article_id)
and people.id not in (
select people.id from articles, referees where <---- **FROM clause is missing here**
people.id = referee_id and refused is null and reported is null and 
editors_decision is null and article_id = articles.id)
and people.id not in (
select people.id from referees where people.id = referee.id and reported is not null
and date_trunc('day', current_timestamp - cast(reported as timestamp)) > 30)

中缺少FROM子句的子查询
select people.id from articles, referees where <---- **FROM clause is missing here**
people.id = referee_id and refused is null and reported is null and 
editors_decision is null and article_id = articles.id

答案 1 :(得分:0)

问题在于(在您的上一个not in区块中):

where people.id = referee.id 

我相信这应该是:

where people.id = referees.id 

编辑:这是我格式化您的查询的方式:

select people.id, lower(lastname) as lname 
from people 
where (interests ~* '$key' 
  or biography ~* '$key'
  or exists (
    select * from authors, articles 
    where articles.keywords ~* '$key' 
    and articles.id=authors.article_id
    and authors.author_id=people.id)) 
and not inactive 
and email is not null
and people.id not in (
  select people.id 
  from people, authors 
  where authors.author_id = people.id 
  and authors.article_id = $article_id)
and people.id not in (
  select people.id 
  from articles, referees 
  where people.id = referee_id 
  and refused is null 
  and reported is null 
  and editors_decision is null 
  and article_id = articles.id)
and people.id not in (
  select people.id 
  from referees 
  where people.id = referee.id 
  and reported is not null
  and date_trunc('day', current_timestamp - cast(reported as timestamp)) > 30)