我正在使用这个:
PreparedStatement preStatement = conn.prepareStatement("SELECT * FROM SomeTable WHERE attributeId = ? AND substring_index(substring_index(rowIdCombo,',',2),',',-1) = ?");
preStatement.setString(1, anAttributeID.toString());
preStatement.setString(2, locationID.toString());
使用相同查询进行搜索在 MySQL 终端上运行良好。只有在 Java
中使用 PreparedStatement
时才不会。
rowIdCombo 基本上是一串带有逗号分隔值的数字。像这样的:23,56,64,3
。
返回的结果集为空。我怎样才能让这个查询工作?
最佳答案
基于
的输出System.out.println(preStatement);
这是:
com.mysql.jdbc.PreparedStatement@223d2c72: SELECT * FROM mydb.SomeTable WHERE attributeId = '6' AND substring_index(substring_index(rowIdCombo,',',2),',',-1) = '1'
根据您的评论,将 = '1' 替换为 = 1 已经解决了问题,为了防止单引号换行,请在 PreparedStatement
中设置值作为整数,使用:
preStatement.setInt(2, Integer.parseInt(locationID.toString()));
关于java - PreparedStatement 不适用于 substring_index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449032/