我该如何修改PostgreSQL的SQL查询?

时间:2008-10-22 15:46:35

标签: postgresql

我有SQL查询,它在Oracle和MSSQL上运行良好。现在我在PostgreSQL上尝试这个,它给出了一个奇怪的例外:org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry for table "main"

以下是查询:

SELECT *
FROM "main" main
     INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
     INNER JOIN "something" somehting ON something_link."somethingid" = something."id"
     INNER JOIN "type" type ON something."typeid" = type."id"

这是一个非常简单的查询,我不明白为什么它不适用于Windows XP SP2,PostgreSQL 8.3?

3 个答案:

答案 0 :(得分:4)

服用点=>东西


postgres=# create database test
postgres-# ;
CREATE DATABASE

postgres=# \c test
You are now connected to database "test".

test=# select version();
                                            version                                            
-----------------------------------------------------------------------------------------------
 PostgreSQL 8.3.3 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)

test=# create table main(id int);
CREATE TABLE

test=# create table something_link(mainid int);
CREATE TABLE

test=# create table something(id int);
CREATE TABLE

test=# create table type(id int);
CREATE TABLE

test=# alter table something add column typeid int;
ALTER TABLE

test=# SELECT *
test-# FROM "main" main
test-#      INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
test-#      INNER JOIN "something" somehting ON something_link."somethingid" = something."id"
test-#      INNER JOIN "type" type ON something."typeid" = type."id"
test-# ;
ERROR:  column something_link.somethingid does not exist
LINE 4:      INNER JOIN "something" somehting ON something_link."som...
                                                 ^
test=# alter table something_link add column somethingid int;
ALTER TABLE

test=# SELECT *                                              
FROM "main" main
     INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
     INNER JOIN "something" *somehting* ON something_link."somethingid" = something."id"
     INNER JOIN "type" type ON something."typeid" = type."id"
;

ERROR:  invalid reference to FROM-clause entry for table "something"
LINE 4: ...hing" somehting ON something_link."somethingid" = something....
                                                             ^
HINT:  Perhaps you meant to reference the table alias "somehting".

test=# SELECT *
FROM "main" main
     INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
     INNER JOIN "something" something ON something_link."somethingid" = something."id"
     INNER JOIN "type" type ON something."typeid" = type."id"
;

 id | mainid | somethingid | id | typeid | id 

----+--------+-------------+----+--------+----

(0 rows)

答案 1 :(得分:4)

真正的问题实际上不是查询,而是PostgreSQL 8.3的默认配置。 在纠正拼写错误(10x Kendrick Wilson)后,问题仍然存在,直到我编辑了“postgresql.conf”文件。应该有一行:

add_missing_from = on

此行确保与其他SQL方言的兼容性。

答案 2 :(得分:2)

根据this,您似乎错误输入别名或使用表名代替它。