H2随机选择LIMIT和OFFSET()

时间:2015-07-09 07:38:53

标签: sql select random h2 offset

在h2 dbms我有一个表

CREATE TABLE sometable
( ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
  somevalue INTEGER)

和一些数据

INSERT INTO sometable(somevalue) 
VALUES (1), (2), (3), (4), (5);

SELECT somevalue FROM sometable LIMIT 1 OFFSET 2 有效时为什么如果我想选择随机行 SELECT somevalue FROM sometable LIMIT 1 OFFSET (RANDOM()*4) 不起作用

1 个答案:

答案 0 :(得分:1)

您可以这样做:

create table test(
  id bigint auto_increment primary key, 
  name varchar(255));

insert into test 
select x, 'Hello ' || x from system_range(50, 1200);

select * from test t, system_range(1, 100) range
where t.id = x * (select max(id)-min(id) from test) / 100 + 
(select min(id) from test);

Source