订单表被编译器读取为ORDER命令

时间:2019-01-22 15:29:53

标签: sql postgresql keyword

我将此SQL语句存储在字符串变量中

SELECT
    o.id_order,
    o.registerdate,
    i.latest_invoice,
    d.latest_delivery 
  FROM
    order o 
  JOIN
    (
      SELECT
        id_address,
        max(registerdate) as latest_invoice 
      FROM
        invoice 
      GROUP BY
        id_address
    ) i     
      ON o.id_address = i.id_address  
  JOIN
    (
      SELECT
        id_address,
        max(registerdate) as latest_delivery 
      FROM
        delivery 
      GROUP BY
        id_address
    ) d      
      ON o.id_address = d.id_address  
  WHERE
    o.id_address = '189'

执行查询时,在order o的位置出现错误,因为编译器认为这是ORDER BY命令。你知道我该怎么避免吗?

1 个答案:

答案 0 :(得分:2)

您不应将保留关键字用作表名,但是如果您坚持要have to use double quotes

from "order" o

请注意,双引号名称区分大小写。根据创建该表的方式,您可能需要"ORDER""Order"。在psql中,您应该使用\d命令检查大小写是否正确。