{"record":{"id":"3739e42c1e6cd893","repo":"baomidou/mybatis-plus","slug":"sql-where-sql-3739e4","errorCode":null,"errorMessage":"非法SQL，where条件中不能使用子查询，错误子查询SQL信息：","messagePattern":"非法SQL，where条件中不能使用子查询，错误子查询SQL信息：","errorType":"exception","errorClass":"MybatisPlusException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/IllegalSQLInnerInterceptor.java","lineNumber":191,"sourceCode":"        if (expression instanceof OrExpression) {\n            OrExpression orExpression = (OrExpression) expression;\n            throw new MybatisPlusException(\"非法SQL，where条件中不能使用【or】关键字，错误or信息：\" + orExpression.toString());\n        } else if (expression instanceof NotEqualsTo) {\n            NotEqualsTo notEqualsTo = (NotEqualsTo) expression;\n            throw new MybatisPlusException(\"非法SQL，where条件中不能使用【!=】关键字，错误!=信息：\" + notEqualsTo.toString());\n        } else if (expression instanceof BinaryExpression) {\n            BinaryExpression binaryExpression = (BinaryExpression) expression;\n            // TODO 升级 jsqlparser 后待实现\n//            if (binaryExpression.isNot()) {\n//                throw new MybatisPlusException(\"非法SQL，where条件中不能使用【not】关键字，错误not信息：\" + binaryExpression.toString());\n//            }\n            if (binaryExpression.getLeftExpression() instanceof Function) {\n                Function function = (Function) binaryExpression.getLeftExpression();\n                throw new MybatisPlusException(\"非法SQL，where条件中不能使用数据库函数，错误函数信息：\" + function.toString());\n            }\n            if (binaryExpression.getRightExpression() instanceof Subtraction) {\n                Subtraction subSelect = (Subtraction) binaryExpression.getRightExpression();\n                throw new MybatisPlusException(\"非法SQL，where条件中不能使用子查询，错误子查询SQL信息：\" + subSelect.toString());\n            }\n        } else if (expression instanceof InExpression) {\n            InExpression inExpression = (InExpression) expression;\n            if (inExpression.getRightExpression() instanceof Subtraction) {\n                Subtraction subSelect = (Subtraction) inExpression.getRightExpression();\n                throw new MybatisPlusException(\"非法SQL，where条件中不能使用子查询，错误子查询SQL信息：\" + subSelect.toString());\n            }\n        }\n\n    }\n\n    /**\n     * 如果SQL用了 left Join，验证是否有or、not等等，并且验证是否使用了索引\n     *\n     * @param joins      ignore\n     * @param table      ignore\n     * @param connection ignore\n     */","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/IllegalSQLInnerInterceptor.java#L173-L209","documentation":"Non-5.0 variant of the subquery rejection: IllegalSQLInnerInterceptor.validExpression sees a Subtraction node on the right of a BinaryExpression in WHERE and throws '不能使用子查询' with the fragment text. The Subtraction check is a heuristic carried over from older JSqlParser where subquery expressions surfaced this way, so genuine arithmetic right-hand sides can also be caught.","triggerScenarios":"Executing WHERE col = (SELECT ...) forms or WHERE with subtraction on the right (WHERE total = price - discount) while IllegalSQLInnerInterceptor from the mybatis-plus-jsqlparser module is registered.","commonSituations":"Reporting SQL with scalar subqueries; price/discount arithmetic in WHERE being misidentified; upgrading jsqlparser dependency versions altering parse shapes.","solutions":["Replace the scalar subquery with a JOIN or a pre-fetched bound parameter","If it is real arithmetic being misclassified, move the arithmetic to Java and bind the result, or restructure the predicate to avoid a Subtraction node on the right","Drop IllegalSQLInnerInterceptor if the rule does not fit the project"],"exampleFix":"-- before\nSELECT * FROM t_sku WHERE price = (SELECT max_price FROM t_config WHERE id = 1);\n-- after\nSELECT s.* FROM t_sku s JOIN t_config c ON s.price = c.max_price WHERE c.id = 1;","handlingStrategy":"validation","validationCode":"// Reject Subtraction-on-right WHERE comparisons before execution\nExpression where = ((PlainSelect) ((Select) CCJSqlParserUtil.parse(sql).getSelectBody()).getSelectBody()).getWhere();\nif (where instanceof BinaryExpression be && be.getRightExpression() instanceof Subtraction) {\n    throw new IllegalArgumentException(\"Subquery-shaped right expression in WHERE\");\n}","typeGuard":"static boolean hasSubtractionRight(Expression where) {\n    return where instanceof BinaryExpression be\n        && be.getRightExpression() instanceof Subtraction;\n}","tryCatchPattern":"try {\n    return mapper.selectWithScalarSub(p);\n} catch (MybatisPlusException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"不能使用子查询\")) {\n        return mapper.selectWithJoin(p);\n    }\n    throw e;\n}","preventionTips":["Bind pre-computed values instead of arithmetic expressions on the comparison's right side","Replace scalar subqueries with joins","Regression-test intercepted mappers after any jsqlparser version bump"],"tags":["mybatis-plus","sql","jsqlparser","subquery","arithmetic"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}