要添加到查询中的其他未指定字段

时间:2010-01-06 14:57:41

标签: java sql postgresql

我有一些查询在java程序中运行,该程序使用PostgreSQL数据库和旧版本JDataStore的某些部分(用于与数据库交互的部分)。有时,一次执行查询就会将查询发送到数据库两次。更奇怪的是,发送的第一个查询与第二个查询略有不同,并且不正确。例如:

First Query (incorrect)
SELECT b."construct_id", c."instance_id", a.SymbolName, c.Address AddressDecimal,
       c.Description, b.ConstructName, a.DeclarationType, a.Symbol_id,
       a.SymbolType_id, a.Construct_id, a.Leaf 
FROM tblSymbolDeclaration a, tblLanguageConstructName b, tblSymbolInstance c  
WHERE a.Construct_id = b.Construct_id and a.Symbol_id = c.Symbol_id
  and a.DeclarationType = 1 and a.Root = 1

请注意该查询开头的两个字段,以及缺少“as”一词,与此相比:

Second Query (correct)
SELECT a.SymbolName, c.Address as AddressDecimal, c.Description, 
       b.ConstructName, a.DeclarationType, a.Symbol_id, a.SymbolType_id,
       a.Construct_id, a.Leaf 
FROM tblSymbolDeclaration a, tblLanguageConstructName b, tblSymbolInstance c 
WHERE a.Construct_id = b.Construct_id and a.Symbol_id = c.Symbol_id 
  and a.DeclarationType = 1 and a.Root = 1

我们有一组我们使用的查询,第一个查询甚至不在该列表中。什么可能导致这个? (对不起,我没有提供任何代码,但在这种情况下这样做是不可行的。)

1 个答案:

答案 0 :(得分:0)

查询不会像这样“改变”。我确定没有在以太网中创建字段名称“instance_id”。我认为构建查询本身是一个错误。

  1. 如何构建查询并将其传递给数据库?
  2. 您在哪里看到“不正确”的查询?
  3. 好的,在黑暗中拍摄,但是为了改变它,尝试将您的查询更改为此。看看服务器告诉你的内容。

    String myQuery = ""
    + "SELECT a.symbolname, "
    + "       c.address AS addressdecimal, "
    + "       c.DESCRIPTION, "
    + "       b.constructname, "
    + "       a.declarationtype, "
    + "       a.symbol_id, "
    + "       a.symboltype_id, "
    + "       a.construct_id, "
    + "       a.leaf "
    + "FROM   tblsymboldeclaration a "
    + "       INNER JOIN tbllanguageconstructname b "
    + "         ON a.construct_id = b.construct_id "
    + "       INNER JOIN tblsymbolinstance c "
    + "         ON a.symbol_id = c.symbol_id "
    + "WHERE  a.declarationtype = 1 "
    + "       AND a.ROOT = 1";