2008-10-22 7 views
2

J'ai une requête SQL, qui fonctionne bien sur Oracle et MSSQL. Maintenant, je suis en train sur ce PostgreSQL et lui donne une étrange exception: org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry for table "main"Comment modifier une requête SQL pour PostgreSQL?

Voici la requête:

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" 

Ceci est assez simple requête et je ne vois pas pourquoi il ne fonctionne pas sur Windows XP SP2, PostgreSQL 8.3?

+0

Plateforme et version? –

+0

Désolé - je l'ai oublié. Windows XP SP2, PostgreSQL 8.3 –

Répondre

2

Selon this, semble que vous soit mal saisi un alias ou utilisé un nom de table en place de celui-ci.

+0

Merci! En fait, il y a eu une faute d'orthographe mais le vrai problème que vous m'avez signalé dans la documentation était différent. Voir mon message ci-dessous. –

4

somehting => quelque chose

 

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

Merci! Il y avait vraiment une faute de frappe! Mais ce n'était pas le premier problème. La prochaine fois que je ne poserai pas de question tard dans la nuit, mais attendez jusqu'au lendemain matin avec un esprit frais :) Je vais expliquer dans la réponse ainsi le prochain problème. –

4

Le vrai problème n'est pas la requête, mais la configuration par défaut de PostgreSQL 8.3. Après avoir corrigé l'erreur d'orthographe (10x Kendrick Wilson), le problème a persisté, jusqu'à ce que j'ai édité le fichier "postgresql.conf". Il doit y avoir une ligne:

add_missing_from = on 

Cette ligne assure la compatibilité avec les autres dialectes SQL.

+0

Non, cela ajoute uniquement le 'missing from', qui peut être ou non compatible avec d'autres dialectes. Content que tu aies résolu ton problème. –

+0

Je veux dire que même avec add_missing_from = on, différents problèmes de compatibilité persistent. En fait, je ne suis même pas sûr que MSSQL et Oracle ajoutent aussi des "manquants" ... Mais vous le dites, donc je vais vous croire :) –

Questions connexes