首页 > 代码库 > 随机从数据库中查出一条语句

随机从数据库中查出一条语句

SELECT * from humor ORDER BY rand() limit 1

SELECT * FROM `user`
WHERE USER_ID >= (SELECT floor(RAND() * (SELECT MAX(USER_ID) FROM `user`)))
ORDER BY USER_ID LIMIT 1 ;

 

Hibernate中,需要使用SQLQuery对象

String sql = "SELECT * from humor ORDER BY rand() limit 1";
// 使用会话获得对象
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Humor.class);

humor = (Humor) query.list().get(0);

 

随机从数据库中查出一条语句