huor
发布于

在OushuDB中模拟order by nulls first和order by nulls last

testdb=# create table t (id int);
CREATE TABLE

testdb=# insert into t values (1);
INSERT 0 1
testdb=# insert into t values (2);
INSERT 0 1
testdb=# insert into t values (3);
INSERT 0 1
testdb=#
testdb=# insert into t values (NULL);
INSERT 0 1

testdb=# select * from t order by id asc;
 id
----
  1
  2
  3

(4 rows)

testdb=# select * from t order by id desc;
 id
----

  3
  2
  1
(4 rows)

testdb=# select * from t order by (id is not null) asc, id;
 id
----

  1
  2
  3
(4 rows)

testdb=# select * from t order by (id is not null) desc, id;
 id
----
  1
  2
  3

(4 rows)

testdb=# select * from t order by (id is not null) asc, id desc;
 id
----

  3
  2
  1
(4 rows)

testdb=# select * from t order by (id is not null) desc, id desc;
 id
----
  3
  2
  1

(4 rows)
评论
    test