Loading

mysql控制limit

public static void main(String[] args) {
        //String sql = "select * from a";
//         String sql = "select * from a limit 300";
        String sql = "select * from (select * from b limit 1000) limit 400";
        try {
            SQLStatement stmt = SQLUtils.parseSingleMysqlStatement(sql);
            if (stmt instanceof SQLSelectStatement) {
                SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
                SQLSelectQuery query = selectStmt.getSelect().getQuery();
                if (query instanceof SQLSelectQueryBlock) {
                    SQLSelectQueryBlock sQLSelectQueryBlock = (SQLSelectQueryBlock) query;
                    SQLLimit sQLLimit = sQLSelectQueryBlock.getLimit();
                    if (sQLLimit == null) {
                        // 在sql
                        sql = sql + "limit 200";
                    }
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(sql);


    }
posted @ 2022-09-27 19:45  Acelin_H  阅读(34)  评论(0编辑  收藏  举报