为什么我从下面的代码中得到此异常?

List<String> getId=jdbcTemplate.queryForList("select Id from MYTable", String.class);
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("sName", "AC");
parameters.addValue("ids", getId);
List<MypojoClass> result = new ArrayList<>();
String query ="select CTime,HTime from MYTable where Type=:sName and Id in((:ids)) and CTime > CONVERT_TZ(date_format(now() - interval 1 month,'%Y-%m-01 00:00:01'),'+00:00', '-05:30');";
result = namedParameterJdbcTemplate.query(query, parameters, ParameterizedBeanPropertyRowMapper.newInstance(MypojoClass.class));


例外情况:

 org.springframework.jdbc.BadSqlGrammarException) org.springframework.jdbc.BadSqlGrammarException:
    PreparedStatementCallback; bad SQL grammar [
    select CTime,HTime from MYTable where Type=? and Id in((?, ?, ?, ?, ?)) and CTime > CONVERT_TZ(date_format(now() - interval 1 month,'%Y-%m-01 00:00:01'),'+00:00', '-05:30');]; nested exception is java.sql.SQLException: Operand should contain 1 column(s)

最佳答案

IN删除多余的括号

SELECT
    CTime,
    HTime
FROM
    MYTable
WHERE
    Type =?
AND Id IN (?, ?, ?, ?, ?)
AND CTime > CONVERT_TZ(date_format(now() - INTERVAL 1 MONTH,'%Y-%m-01 00:00:01' ),'+00:00','-05:30');


注意:

SELECT '1' IN ('2','1','3'); Result: 1(TRUE) See demo

SELECT '1' IN (('2','1','3')); Result: [Err] 1241 - Operand should contain 1 column(s) See demo

SELECT ('2','1','3') IN (('2','1','3')); Result: 1(TRUE) See demo

关于mysql - java.sql.SQLException:操作数应包含1列-Spring,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38343914/

10-10 03:08