错误FROM关键字未找到预期的位置

时间:2015-08-13 18:50:46

标签: java mysql sql syntax-error

我正在处理一些选择查询,并且在上次查询中找不到错误的FROM关键字,我无法弄清问题是什么。

ResultSet rs4 = stmt.executeQuery("SELECT COUNT(UnitPrice) AS 'Total'"
    + "FROM" 
    + " orderdetails INNER JOIN orders ON orderdetails.OrderID = orders.OrderID"
    + " WHERE customerID = 'LILAS'"
    + "GROUP BY customerid"
    + "HAVING SUM((UnitPrice * Quantity) - (UnitPrice * Quantity * discount)) > "
    + " ( SELECT AVG(SUM((UnitPrice * Quantity) - (UnitPrice * Quantity * discount)))"
    + "AS avg('AMOUNT')"
    + "FROM orderdetails"
    +" GROUP BY UnitPrice, Quantity, Discount)");

System.out.println("# of Orders for this customer < average order amount for all customers =" + rs4.getString(1));

1 个答案:

答案 0 :(得分:0)

错误1:您的字符串连接中似乎缺少AS 'Total'""FROM"之间的空格,这导致语法错误。

错误2:在"AS avg('AMOUNT')行中,avgAVERAGE函数的保留关键字。您当前的用法是尝试调用函数调用。因此,如果要将列别名设置为“avg(AMOUNT)”,请尝试将该行更改为 "AS 'avg(AMOUNT)'