2013-11-25 101 views
0

下,相当复杂,查询生成错误:缺少FROM子句错误

ERROR: missing FROM-clause entry for table "referee" LINE 14: ... select people.id from referees where 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指的是据称丢失的裁判表。此外,选择分句的结构似乎与上面的一样。

我想我错过了一些明显的东西,但我看不到什么。有任何想法吗?

[这是使用PostgreSQL 9.0.3虽然PHP正在执行,因此变量引用以$在上面的SQL开始]

+1

你可能要开始使用正确的缩进格式查询 - 在很多情况下已经揭示了问题的根源。 –

+0

添加别名也会有所帮助。 – joop

回答

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