{"record":{"id":"83aba01f61400d90","repo":"baomidou/mybatis-plus","slug":"sql-where-or-or-83aba0","errorCode":null,"errorMessage":"非法SQL，where条件中不能使用【or】关键字，错误or信息：","messagePattern":"非法SQL，where条件中不能使用【or】关键字，错误or信息：","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":175,"sourceCode":"        List<Join> joins = delete.getJoins();\n        validWhere(where, table, (Connection) obj);\n        validJoins(joins, table, (Connection) obj);\n    }\n\n    /**\n     * 验证expression对象是不是 or、not等等\n     *\n     * @param expression ignore\n     */\n    private void validExpression(Expression expression) {\n        while (expression instanceof ParenthesedExpressionList) {\n            ParenthesedExpressionList<Expression> parenthesis = (ParenthesedExpressionList) expression;\n            expression = parenthesis.get(0);\n        }\n        //where条件使用了 or 关键字\n        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) {","sourceCodeStart":157,"sourceCodeEnd":193,"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#L157-L193","documentation":"Thrown by IllegalSQLInnerInterceptor.validExpression when the WHERE expression (after unwrapping ParenthesedExpressionList) is an OrExpression. The interceptor enforces a SQL standard that forbids OR in WHERE conditions — typically because OR defeats single-index usage — and appends the offending expression text after '错误or信息：'.","triggerScenarios":"Registering IllegalSQLInnerInterceptor and executing any statement whose top-level WHERE contains OR, e.g. WHERE a = 1 OR b = 2, including parenthesized (a = 1 OR b = 2) since parenthesis wrappers are unwrapped before the check.","commonSituations":"Search/filter screens built with OR conditions; QueryWrapper.eq(...).or().eq(...) chains; legacy mappers using OR after the interceptor is introduced to enforce index discipline.","solutions":["Rewrite OR over the same column as IN: WHERE status = 1 OR status = 2 becomes WHERE status IN (1, 2)","Rewrite cross-column OR as UNION / UNION ALL of two indexed queries","Remove or disable IllegalSQLInnerInterceptor if OR conditions are an accepted part of your query patterns"],"exampleFix":"-- before\nSELECT * FROM t_order WHERE status = 1 OR status = 2;\n-- after\nSELECT * FROM t_order WHERE status IN (1, 2);","handlingStrategy":"try-catch","validationCode":"// Detect OR at the top of a WHERE before execution\nExpression where = ((PlainSelect) ((Select) CCJSqlParserUtil.parse(sql).getSelectBody()).getSelectBody()).getWhere();\nwhile (where instanceof ParenthesedExpressionList<?> p) { where = (Expression) p.get(0); }\nif (where instanceof OrExpression) {\n    throw new IllegalArgumentException(\"OR in WHERE blocked by IllegalSQLInnerInterceptor\");\n}","typeGuard":"static boolean isOrExpression(Expression where) {\n    return where instanceof OrExpression;\n}","tryCatchPattern":"try {\n    return mapper.search(a, b);\n} catch (MybatisPlusException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"【or】\")) {\n        return mapper.searchByIn(a, b); // IN or UNION variant\n    }\n    throw e;\n}","preventionTips":["Prefer IN over same-column OR; prefer UNION over cross-column OR","Avoid QueryWrapper .or() chains when the interceptor is enabled","Add a unit test that parses mapper XML and asserts no OrExpression appears at WHERE top level"],"tags":["mybatis-plus","sql","or-condition","interceptor","index"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}