限制最小最大范围值的sql查询语法
问题描述:
我是新来的。我在我的SQL查询中有一些问题。我在我的java代码中写入sql查询,从用户获取三个值(限制,最小值,最大值),并显示最小和最大范围之间的记录。例如,如果用户想要查看10条记录并且在5到100个交易值之间。我正在努力弄清查询的正确语法。我的查询是限制最小最大范围值的sql查询语法
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..
您的帮助将是非常赞赏。我真的非常挣扎。我之前使用过简单的查询,但知道如何解决该错误。 非常感谢你提前。
答
min
和max
只是您查询中的范围值 - 不需要应用聚合函数。此外,limit
条款应该位于where
条款后:
SELECT Transaction_ID, Transaction_Value, Transaction_Type, Description FROM Banking
WHERE Transaction_Value >= ? AND
Transaction_Value <= ?
LIMIT ?
答
.... 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;
限制必须出现在where子句 – amdixon
您的基本SQL语法中有多个错误。我建议你阅读一些关于该语言的基本材料,直到你明白“limit”是最后一个语句,并且聚合函数不属于where语句。 –
“我有一些问题”确实如此 – Strawberry