无法加入2个表

时间:2016-04-02 13:34:41

标签: java sql postgresql jdbc

在我们的查询中,我们有2个表要加入。但是我们收到以下错误:

 org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry
 for table "component"   Position: 48   at
 org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270)
    at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:406)
 at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:286)   at datab.Datab.testDB(Datab.java:73)    at datab.Datab.main(Datab.java:60)

这是我的代码:

public static void testDB(Connection con){
  //RUN AN EXAMPLE QUERY
  try {
    if (!con.isClosed())
    {
      Statement st = con.createStatement();
      String query = "SELECT * FROM \"Component\" inner join \"Kind\" on Component.Kind_ID = Kind.ID";
      ResultSet rs = st.executeQuery(query);                

      while (rs.next())
      {                 
        System.out.println(rs.getString(1) + " "  + rs.getString(2) + " " + rs.getString(3));                            
      }
    }                    
  } catch (SQLException e) {
      e.printStackTrace();
  }
}

1 个答案:

答案 0 :(得分:7)

这是您的SQL语句:

SELECT * 
FROM "Component" 
   inner join "Kind" on Component.Kind_ID = Kind.ID

表名"Component"是与component不同的表名。一旦开始使用这些可怕的带引号的标识符,就必须使用它们无处不在。您没有向我们展示您的表定义,但我怀疑您还需要引用列名:

SELECT * 
FROM "Component" 
   inner join "Kind" on "Component"."Kind_ID" = "Kind"."ID"

如果您可以更改此设置,我强烈建议您使用双引号重新创建不带的表格。

有关引用标识符的更多详细信息,请参见手册:
http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS