我们正在使用oracle数据库。我需要按顺序排序结果。所以我创建了一个先对id进行排序的查询,然后我对子查询结果应用了查询以获得不同的段落。
查询是:
select distinct(para)
from (
select para, id
from book
where title='2'
and chapter_id='2'
and subchap='1'
and para is not null
order by id)
order by para
此返回列表为:
a
aa
aaa
bb
c
d
dd
但我希望这个列表为:
a
c
d
aa
bb
dd
aaa
我想列出不同的参赛名单?如何使用oracle查询实现此目的?
答案 0 :(得分:1)
也许只想先按长度排序(至少这种方法符合你的例子):
select distinct(para) from
(select para, id from book where title='2' and chapter_id='2' and subchap='1' and para is not null)
order by length(para), para
或许你想按id排序,在这种情况下,你需要这样的东西:
select para from
(select para, min(id) as minid from book where title='2' and chapter_id='2' and subchap='1' and para is not null group by para)
order by minid
答案 1 :(得分:1)
如果您希望按其ID排序para
。您可以尝试以下查询:
SELECT para
FROM book
WHERE title='2'
and chapter_id='2'
and subchap='1'
and para is not null
GROUP BY para
ORDER BY MIN(id)
请注意,它会在MIN(id)
上排序,因此如果para
有多个ID,则会按最小的ID进行排序。
如果para, id
组合是唯一的,您可以按两个字段进行分组:
SELECT para
FROM book
WHERE title='2'
and chapter_id='2'
and subchap='1'
and para is not null
GROUP BY para, id
ORDER BY id