ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## GROUP分组查询和Having子句 有时候我们要进行分组查询,如以下SQL: ``` SELECT * FROM `user` WHERE `id` IN(1637, 1639, 1640 ) GROUP BY name ``` 在BeetlSql中直接拼条件调用group方法,传入字段即可: ``` Query<User> query = sqlManager.query(User.class); List<User> list = query .andIn("id", Arrays.asList(1637, 1639, 1640)) .groupBy("name") .select(); ``` 在分组查询之后,我们可能还要进行having筛选,只需要在后面调用having方法,传入条件即可。 ``` SELECT * FROM `user` WHERE `id` IN( 1637, 1639, 1640 ) GROUP BY name HAVING `create_time` IS NOT NULL ``` ``` Query<User> query = sqlManager.query(User.class); List<User> list = query .andIn("id", Arrays.asList(1637, 1639, 1640)) .groupBy("name") .having(query.condition().andIsNotNull("create_time")) .select(); ```