baomidou/mybatis-plus · error · MybatisPlusException
非法SQL,where条件中不能使用子查询,错误子查询SQL信息:{}
Error message
非法SQL,where条件中不能使用子查询,错误子查询SQL信息:{} What it means
IllegalSQLInnerInterceptor (jsqlparser 4.9) throws when the RIGHT side of a binary WHERE condition is a Subtraction node. The interceptor treats an arithmetic expression on the value side (col = a - b, a pattern that often appears with subquery rewrites or dynamic SQL) as a forbidden subquery-like construct and rejects it with the expression text appended. Note: this check keys on jsqlparser's Subtraction class, so it actually fires for '-' expressions on the right side.
Source
Thrown at mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/IllegalSQLInnerInterceptor.java:192
if (expression instanceof OrExpression) {
OrExpression orExpression = (OrExpression) expression;
throw new MybatisPlusException("非法SQL,where条件中不能使用【or】关键字,错误or信息:" + orExpression.toString());
} else if (expression instanceof NotEqualsTo) {
NotEqualsTo notEqualsTo = (NotEqualsTo) expression;
throw new MybatisPlusException("非法SQL,where条件中不能使用【!=】关键字,错误!=信息:" + notEqualsTo.toString());
} else if (expression instanceof BinaryExpression) {
BinaryExpression binaryExpression = (BinaryExpression) expression;
// TODO 升级 jsqlparser 后待实现
// if (binaryExpression.isNot()) {
// throw new MybatisPlusException("非法SQL,where条件中不能使用【not】关键字,错误not信息:" + binaryExpression.toString());
// }
if (binaryExpression.getLeftExpression() instanceof Function) {
Function function = (Function) binaryExpression.getLeftExpression();
throw new MybatisPlusException("非法SQL,where条件中不能使用数据库函数,错误函数信息:" + function.toString());
}
if (binaryExpression.getRightExpression() instanceof Subtraction) {
Subtraction subSelect = (Subtraction) binaryExpression.getRightExpression();
throw new MybatisPlusException("非法SQL,where条件中不能使用子查询,错误子查询SQL信息:" + subSelect.toString());
}
} else if (expression instanceof InExpression) {
InExpression inExpression = (InExpression) expression;
if (inExpression.getRightExpression() instanceof Subtraction) {
Subtraction subSelect = (Subtraction) inExpression.getRightExpression();
throw new MybatisPlusException("非法SQL,where条件中不能使用子查询,错误子查询SQL信息:" + subSelect.toString());
}
}
}
/**
* 如果SQL用了 left Join,验证是否有or、not等等,并且验证是否使用了索引
*
* @param joins ignore
* @param table ignore
* @param connection ignore
*/View on GitHub (pinned to bf67d90747)
Solutions
- Compute the arithmetic in application code and bind the result as a parameter: WHERE amount = ?.
- If the expression must stay in SQL, annotate the mapper method with @InterceptorIgnore(illegalSql = "true").
- If you genuinely need a scalar subquery there, it is disallowed by policy — refactor to two statements or a JOIN.
Example fix
-- before
WHERE amount = price - discount
-- after
-- compute (price - discount) in Java, then
WHERE amount = #{computedValue} Defensive patterns
Strategy: validation
Validate before calling
// CI guard: reject arithmetic/subquery-like right-hand expressions in WHERE
// git grep -nE "=\\s*\\w+\\s*-\\s*\\w+" -- src/main/resources/mapper
// prefer parameter placeholders: WHERE amount = #{computed} Try / catch
try {
mapper.selectList(wrapper);
} catch (MybatisPlusException e) {
if (String.valueOf(e.getMessage()).contains("不能使用子查询")) {
log.error("move computation/subquery to application side: {}", e.getMessage());
}
} Prevention
- Bind computed values as parameters instead of inline arithmetic in SQL.
- Avoid .apply() with raw expressions; compute in Java.
- Mark intentional exceptions with @InterceptorIgnore and document why.
When it happens
Trigger: SQL like WHERE amount = price - discount (or a subselect rewritten/normalized as subtraction) in a binary comparison, executed with the interceptor registered.
Common situations: Wrapper .apply("amount = price - discount") fragments; hand-written SQL with arithmetic on the right of a comparison; jsqlparser's model making this branch overlap with subquery detection after version changes.
Related errors
- 非法SQL,where条件中不能使用【or】关键字,错误or信息:{}
- 非法SQL,where条件中不能使用【!=】关键字,错误!=信息:{}
- 非法SQL,where条件中不能使用数据库函数,错误函数信息:{}
- 非法SQL,SQL未使用到索引, table:{}, columnName:{}
- 非法SQL,where条件中不能使用【or】关键字,错误or信息:{}
AI-assisted analysis of baomidou/mybatis-plus@bf67d90747 (2026-08-14).
Data as JSON: /api/errors/6e1d16d786a8f550.
Report an issue: GitHub.