H2 select LIMIT 和 OFFSET with random()

H2 select LIMIT and OFFSET with random()

在 h2 dbms 中我有一个 table

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随机排 SELECT somevalue FROM sometable LIMIT 1 OFFSET (RANDOM()*4) 不起作用?

你可以这样做:

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