How to use the SQL RANDOM() function in a Netbeans JavaDB

人盡茶涼 提交于 2019-12-06 11:00:23

Java DB uses SQL's OFFSET/FETCH syntax to get this functionality.

Assuming RANDOM() orders them correctly the following syntax should work:

SELECT * FROM JAVA2.FORTUNES ORDER BY RANDOM() OFFSET 0 ROWS FETCH NEXT 1 ROW ONLY

From http://db.apache.org/derby/faq.html#limit

Derby does not support the LIMIT syntax. However, Derby 10.4 added the ROW_NUMBER function and Derby 10.7 added the OFFSET and FETCH clauses.

Derby also supports limiting the number of rows returned by a query through JDBC. For example, to fetch the first 5 rows of a large table:

Statement stmt = con.createStatement();  
stmt.setMaxRows(5); 
ResultSet rs = stmt.executeQuery("SELECT * FROM myLargeTable");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!