我是新来的。我的SQL查询中有一些问题。我在我的java代码中编写sql查询,它从用户获取三个值(limit,min,max)并显示最小和最大范围之间的记录。如果用户想要查看10条记录并且在5到100之间的交易值。我正在努力弄清楚查询的正确语法。我的问题是
PreparedStatement statement = con.prepareStatement(""
+ "Select Transaction_ID, Transaction_Value, Transaction_Type, Description "
+ "from Banking "
+ "limit ? "
+ "where MIN(Transaction_Value) = ? AND "
+ "MAX(Transaction_Value) = ? ");
//setting the limit of records, minimum and maximum values specified by the user
statement.setInt(1, num);
statement.setInt(2, min);
statement.setInt(3, max);
ResultSet result = statement.executeQuery();
执行此查询时,会提示错误消息
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MIN(Transaction_Value) = 5 AND MAX(Transaction_Value) = 100' at line 1..
非常感谢您的帮助。我真的非常挣扎。我之前使用过简单查询,但dot知道如何修复该错误。 非常感谢你。
答案 0 :(得分:0)
min
和max
只是查询中的范围值 - 无需应用聚合函数。此外,limit
子句应位于where
子句之后:
SELECT Transaction_ID, Transaction_Value, Transaction_Type, Description FROM Banking
WHERE Transaction_Value >= ? AND
Transaction_Value <= ?
LIMIT ?
答案 1 :(得分:0)
... WHERE Transaction_Value BETWEEN 5 AND 100;
MIN和MAX应始终在GROUP BY子句中用于超过1行
SELECT MAX(daily_typing_pages)
FROM employee_tbl;
SELECT id, name, MAX(daily_typing_pages)
FROM employee_tbl GROUP BY name;